本文介绍了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:
推荐答案
您要传入 $ connection-> myconn
而不是 $ connection
.如:
$result = mysqli_query($connection->myconn, $query);
就目前而言,您正在传递的是类的实例,而不是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,给定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!