0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource problem

I am a beggiinner when it cames to programming with php. At the moment I am working at a project with a database. A problem appears when I try to call my query: $r = mysqli_query($db, $q); I get this error: enter image description here

This is my class where I connect to my database:

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!');
 }
}

And here I call my query:

$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>';
}
Community
  • 1
  • 1
Nistor Alexandru
  • 5,309
  • 9
  • 46
  • 71
  • which debbugger are you using – dynamic Jan 13 '13 at 11:45
  • It says on php.net : mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ) so first parameter is suppose to be link not object – v0d1ch Jan 13 '13 at 11:53

1 Answers1

1

You are giving the instance of conectDB class instead of the link returned by mysqli_connect.

You may do something like that (note the return you are missing) :

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

and then use it

$db = new conectDB();
$q='SELECT * FROM categories ORDER BY category';
$r = mysqli_query($db->getLink(), $q);

On a side node, you should not use the var keyword anymore : http://php.net/manual/en/language.oop5.properties.php

magnetik
  • 4,351
  • 41
  • 58