警告:mysqli_query()期望参数1为mysqli,在[类文件]中给定的对象 [英] Warning: mysqli_query() expects parameter 1 to be mysqli, object given in [class file]

查看:40
本文介绍了警告:mysqli_query()期望参数1为mysqli,在[类文件]中给定的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习OOP,所以我可能做过一些愚蠢的事情.我有使用mysql的代码,但是读到mysqli更好,特别是因为我想从数据库中获取对象数组.我已经更新了代码以尝试现在与mysqli一起使用,并解决了一些问题,但现在我遇到了麻烦.

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><?
    }
}

转储中包含许多我不会在此处粘贴的var,但前几行是

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 */
}
?>

该var_dump产生以下内容:

That var_dump produces the following:

object(Database)#2 (0) { } 

同样,这段代码在我使用mysql语句时都可以使用,但是转换为mysqli困扰了我.

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

我已经读过几个关于此错误的Stack Overflow答案-给定对象"-几十个像给定字符串"之类的不适用的错误.我不知道自己做错了什么.

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.

推荐答案

我不会尝试在构造函数中返回任何内容.为什么不调用单独的功能来获得连接?

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));
    

这篇关于警告:mysqli_query()期望参数1为mysqli,在[类文件]中给定的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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