0

可能重复:
mysql_fetch_array() 期望参数 1 是资源问题

当谈到使用 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>';
}
4

1 回答 1

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

于 2013-01-13T11:53:18.003 回答