本文介绍了mysqli_query() 期望参数 1 是 mysqli,给定的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可用于连接到 MySQL 数据库的类.这是我的代码:

I'm trying to create a class which can be used for connecting to MySQL database. This is my code:

班级:

<?php

class createCon  {
    var $host = 'localhost';
    var $user = 'root';
    var $pass = '';
    var $db = 'example';
    var $myconn;

    function connect() {
        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
        if (!$con) {
            die('Could not connect to database!');
        } else {
            $this->myconn = $con;
            echo 'Connection established!';}
        return $this->myconn;
    }

    function close() {
        mysqli_close($myconn);
        echo 'Connection closed!';
    }

}

这是我尝试查询数据库的地方:

And this is where I try to query the database:

<?php

include 'connect.php';

$connection = new createCon();
$connection->connect();

$query = 'SELECT * FROM  `nickname`';
$result = mysqli_query($connection, $query);

if($numrows = mysqli_num_rows($result)) {
    echo $numrows;
    while ($row = mysqli_fetch_assoc($result)) {
        $dbusername = $row['nick'];
        $dbpassword = $row['pass'];
        echo $dbusername;
        echo $dbpassword;
    }
}

当我尝试进行查询时出现以下错误:

I get the following error when I try to make a query:

警告:mysqli_query() 期望参数 1 是 mysqli,对象在 C:wampwwwuppgift 1 kompleteringest.php 第 13 行

推荐答案

您想传入 $connection->myconn 而不是 $connection.如:

You want to pass in $connection->myconn instead of $connection. As in:

$result = mysqli_query($connection->myconn, $query);

就目前而言,您传入的是类的一个实例,而不是 mysqli,而 mysqli 正是错误消息所抱怨的.

As it stands, you're passing in an instance of your class, rather than a mysqli, which is what the error messages are complaining about.

这篇关于mysqli_query() 期望参数 1 是 mysqli,给定的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:26
查看更多