可能重复:
mysql_fetch_array() expects parameter 1 to be resource problem
当使用php进行编程时,我是一名乞丐。目前我正在一个拥有数据库的项目工作。当我尝试调用我的查询时出现问题:$r = mysqli_query($db, $q);
我收到此错误:
这是我连接数据库的课程:
class conectDB{
var $dbUser;
var $dbPassword;
var $dbHost;
var $dbName;
function __construct() {
$this->dbUser ='root';
$this->dbPassword = '';
$this->dbHost = 'localhost';
$this->dbName = 'db';
$dbc = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName) or die('Fatal error!');
}
}
在这里我打电话给我的问题:
$db = new conectDB();
$q='SELECT * FROM categories ORDER BY category';
$r = mysqli_query($db, $q);
while(list($id, $category) = mysqli_fetch_array($r, MYSQLI_NUM)){
echo '<li><a href="category.php?id='.$id.'" title="'.$category.'">'.$category.'</a></li>';
}
答案 0 :(得分:1)
您正在提供conectDB
类的实例,而不是mysqli_connect
返回的链接。
你可能会做那样的事情(注意你缺少的回报):
class conectDB {
protected $dbUser;
protected $dbPassword;
protected $dbHost;
protected $dbName;
protected $link;
function __construct() {
$this->dbUser ='root';
$this->dbPassword = '';
$this->dbHost = 'localhost';
$this->dbName = 'db';
$this->link = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName) or die('Fatal error!');
}
public function getLink() {
return $this->link;
}
}
然后使用它
$db = new conectDB();
$q='SELECT * FROM categories ORDER BY category';
$r = mysqli_query($db->getLink(), $q);
在辅助节点上,您不应再使用var
关键字:http://php.net/manual/en/language.oop5.properties.php