PHP mysqli_query() expects parameter 1 to be mysqli, object given in [duplicate]

一世执手 提交于 2021-02-01 05:13:30

问题


I have this code

class Db_conn {
    private $sn = 'localhost';
    private $un = 'root';
    private $up = '';
    public function connect(string $db_n){
        $conn = mysqli_connect($this->sn, $this->un, $this->up, $db_n);
        if (!$conn) {
            die("Připojení se nezdařilo: " . mysqli_error($conn));
        } else {
            return $conn;
        }
    }
}

And this code

public function update($query){
    $dbconn = new Db_conn();
    if (mysqli_query($dbconn, $query)) {
        return True;
    } else {
        return False;
    }
}

And on this line if (mysqli_query($dbconn, $query)) { it says there is an error.

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in D:\xampp\htdocs\purkiada2\content\Db_parser.inc.php on line 21


回答1:


mysqli_query expects a mysqli connection, but you give it a Db_conn object. This is what the error message says.

You must first connect and then give this (mysqli) connection to mysqli_query, e.g.

public function update($query){
    $dbconn = new Db_conn();
    $conn = $dbconn->connect();
    if (mysqli_query($conn, $query)) {
        return True;
    } else {
        return False;
    }
}


来源:https://stackoverflow.com/questions/41815734/php-mysqli-query-expects-parameter-1-to-be-mysqli-object-given-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!