tags:

views:

58

answers:

4

Okay I have a piece of code that for some reason gives me the following error.

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

Here is the code.

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM sitename WHERE id='$user_id'");
while($row = mysqli_fetch_array($dbc)){ 
$state = $row["state"];
$city = $row["city"];
$zip = $row["zip"]; 
$bio_body = $row["bio_body"]; 
}

If you can please help me by giving me the correct code.

+3  A: 

Your mysqli_query returned false (which means error in query in general). Check mysql errors before do mysqli_fetch_array

Alexey Sviridov
Thx, acrosman, my english is ugly :)
Alexey Sviridov
A: 

from what i remember as u are using new mysqli

should u be using $mysqli->query() and $mysqli->fetch_array() instead of the traditional methods?

or u can just remove the new keyword and keep the rest the same

Sabeen Malik
You can use the mysqli commands in two different ways: procedural and OO; they have different syntax.
Anax
@Anax .. yes that part i know .. but i am not sure if u can mix them both?
Sabeen Malik
A: 

Does you code actually call "SELECT * FROM sitename WHERE id='$user_id'"? I would imagine the table name is users or members or something.

piddl0r
+1  A: 

To think about how to do something intellegent with your errors take a look at the mysqli error function. This code should run, and print out errors, but it's not a good way to leave the system in the end (also see the comment above about blank passwords).

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM sitename WHERE id='$user_id'");
if (!$dbc) {
    // There was an error...do something about it here...
    print mysqli_error();
}  else {
    while($row = mysqli_fetch_array($dbc)){ 
        $state = $row["state"];
        $city = $row["city"];
        $zip = $row["zip"]; 
        $bio_body = $row["bio_body"]; 
    }
}

One final note, if you're getting started with PHP and databases, I'd strongly suggest you look into PDO. It provides a level of data abstraction absent in the old MySQL and MySQLi functions and objects.

acrosman