我需要将函数中的所有PDO都转换为MYSQLI,但找不到PDO替代品 [英] I need to convert all the PDO in my function to MYSQLI but can't find the PDO alternatives

查看:95
本文介绍了我需要将函数中的所有PDO都转换为MYSQLI,但找不到PDO替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在下面的switch语句中寻找一种方法来迷住我的PDO :: PARAM_INT和其他pdo值,以使用mysqli.我正在做一个投资组合项目,并且在学校里,发现由于某些原因我们不允许使用PDO,所以现在我必须重新组织10页.提前致谢!

I am trying to find a way to convet my PDO::PARAM_INT and the other pdo values in the switch statement below to use mysqli. I am doing a portfolio project and for school and found out we are not allowed to use PDO for some reason so now I have to restructure 10 pages. Thanks in advance!

public function bind($param, $value, $type = null){
    if(is_null($type)){
        switch(true){
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
                default:
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($param, $value, $type);
}

推荐答案

mysqli有一个称为

mysqli has a method called bind_param(). It works quite different, so it is not so easy to replace your code with it. The first parameter passed to bind_param() is a string composed of letters signifying the type of parameters. For the most part you can use s for all types. In your case you would need to map booleans to integers presumably. MySQL doesn't have a true boolean data type so all booleans are saved as 0 or 1.

示例如下:

$int = 2;
$bool = (int) false;
$string = '0';

$stmt = $mysqli->prepare('INSERT INTO dates VALUES(?,?,?)');
$stmt->bind_param('sss', $int, $bool, $string);
$stmt->execute();

所有参数必须绑定在对bind_param单个调用中,并且变量必须通过引用传递,因此它们不能为文字.

All parameters must be bound in a single call to bind_param and the variables must be passed by reference, so they can't be literals.

您不能只用mysqli等效项替换您的方法.您将需要重写代码逻辑.我强烈建议您坚持使用PDO;它比mysqli更容易和更好.如果必须使用mysqli,则应该可能编写或使用现有的数据库抽象类.不建议单独使用mysqli.

You can't just replace your method with a mysqli equivalent. You would need to rewrite your code logic. I would strongly recommend to stick with PDO; it is easier and better than mysqli. If you must use mysqli, then you should probable write or use an existing database abstraction class. Using mysqli on its own is not recommended.

这是此类类的示例:

class DBClass extends mysqli {
    public function __construct(
        $host = null,
        $username = null,
        $passwd = null,
        $dbname = null,
        $port = null,
        $socket = null
    ) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
        $this->set_charset('utf8mb4');
    }

    public function safeQuery(string $sql, array $params = []): ?array {
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        if ($result = $stmt->get_result()) {
            return $result->fetch_all(MYSQLI_BOTH);
        }
        return null;
    }
}

$int = 2020;
$bool = false;
$string = '0';

$conn = new DBClass('localhost', 'inet', '5432', 'test');
$conn->safeQuery('INSERT INTO dates VALUES(?,?,?)', [$int, (int) $bool, $string]);

var_dump($conn->safeQuery('SELECT * FROM dates WHERE year=2020'));

这绝对不是有史以来最好的类,但是我用它来说明如何使用一种更简单的辅助方法扩展mysqli.它具有与mysqli类相同的构造函数,但是该构造函数启用错误报告并设置适当的字符集.新方法只是麻烦的准备/绑定/执行模式的包装.它应该适用于各种查询.

This is by means not the best class ever, but I use it to illustrate how one could go about extending mysqli with a simpler helper method. It has the same constructor as mysqli class, but the constructor enables error reporting and sets the proper charset. The new method is just a wrapper around the cumbersome prepare/bind/execute pattern. It should work for all kinds of queries.

这篇关于我需要将函数中的所有PDO都转换为MYSQLI,但找不到PDO替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆