Warning: mysqli_query() expects parameter 1 to be mysqli, object given in [class file] [duplicate]

北慕城南 提交于 2021-02-08 10:14:39

问题


I'm learning OOP, so I've probably done something stupid. I had code working with mysql, but read that mysqli is better, especially since I'm wanting to get an array of objects from the database. I've updated my code to try and work with mysqli now, and have solved some problems, but now I'm stuck.

From my database class file:

class Database {

    function __construct() {
        $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die(mysqli_error($db));
        ?><pre><?
        var_dump($connection);
        ?></pre><?
    }
}

The dump includes a number of vars which I won't paste here, but the first few lines:

object(mysqli)#3 (19) {
  ["affected_rows"]=>
  int(0)
  ["client_info"]=>
  string(6) "5.5.40"
  ["client_version"]=>
  int(50540)
  ["connect_errno"]=>
  int(0)
  ["connect_error"]=>
  NULL

I believe this means I'm successfully connecting to the database.

So here's from my member class file:

<?php

include_once (inc('_class.db'));

class Member {

    //Database connect 
    public function __construct() {
        $db = new Database();
    }

/*snip a registration function */

    // Login process
    public function check_login($emailusername, $password) {
        $db = new Database(); # added for troubleshooting, seems to do nothing...
        var_dump($db);

        $password = md5($password);
        $result = mysqli_query($db,"SELECT * from members WHERE email = '$emailusername' or username='$emailusername' and password = '$password'") or die(mysqli_error($db));
        $user_data = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $no_rows = mysqli_num_rows($result);

/* snip code that runs after that */
}
?>

That var_dump produces the following:

object(Database)#2 (0) { } 

Again, this code all worked when I was using mysql statements, but converting to mysqli buggered me.

I've read several Stack Overflow answers that were this error - "object given" - and a couple dozen that were others like "string given" that didn't apply. I can't figure out what I've done wrong.

As far as I can tell, this is not a duplicate question, even though questions like it are asked often.


回答1:


I wouldnt try to return anything inside the constructor. Why not call a separate fuction to get the connection?

 class Database {

    function __construct() {}
    function getConnection(){
        $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
        return $connection;
    }
}

and then get the connection from the Database object after instantiation:

$result = mysqli_query($db->getConnection(),"SELECT * from members WHERE email = '$emailusername' or username='$emailusername' and password = '$password'") or die(mysqli_error($db));
    



回答2:


You cannot pass an instance of your custom Database class into mysqli_query. It needs a specific connection, not an instance of an arbitrary class you've defined.

You need to get your $connection into mysqli_query, and the easiest way is probably to add a wrapper for mysqli_query to your custom class:

class Database {
    private $connection;

    function __construct() {
        $this->connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die(mysqli_error($db));
    
    }

    function query($query_string) {
        return mysqli_query($this->connection, $query_string);
    }
}


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

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