1

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

When I run my php page, I get this error and do not know what's wrong, can anyone help? If anyone needs more infomation, I'll post the whole code.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
H:\Program Files\EasyPHP 2.0b1\www\test\info.php on line 16
<?PHP

    $user_name = "root";
    $password = "";
    $database = "addressbook";
    $server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

    $SQL = "SELECT * FROM tb_address_book";
    $result = mysql_query($SQL);

    while ($db_field = mysql_fetch_assoc($result)) {
        print $db_field['ID'] . "<BR>";
        print $db_field['First_Name'] . "<BR>";
        print $db_field['Surname'] . "<BR>";
        print $db_field['Address'] . "<BR>";
    }    

    mysql_close($db_handle);

}
else {
    print "Database NOT Found ";
    mysql_close($db_handle);
}

?>
Community
  • 1
  • 1

2 Answers2

11

It generally means that you've got an error in your SQL.

$sql = "SELECT * FROM myTable"; // table name only do not add tb
$result = mysql_query($sql);
var_dump($result);    // bool(false)

Obviously, false is not a MySQL resource, hence you get that error.

EDIT with the code pasted now:

On the line before your while loop, add this:

if (!$result) {
    echo "Error. " . mysql_error();
} else {
    while ( ... ) {
       ...
    }
}

Make sure that the tb_address_book table actually exists and that you've connected to the DB properly.

mdurant
  • 27,272
  • 5
  • 45
  • 74
nickf
  • 537,072
  • 198
  • 649
  • 721
0
<?PHP

    $user_name = "root";
    $password = "";
    $database = "addressbook";
    $server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

    $SQL = "SELECT * FROM tb_address_book";
    $result = mysql_query($SQL);

    while ($db_field = mysql_fetch_assoc($result)) {
        print $db_field['ID'] . "<BR>";
        print $db_field['First_Name'] . "<BR>";
        print $db_field['Surname'] . "<BR>";
        print $db_field['Address'] . "<BR>";
    }    

    mysql_close($db_handle);

}
else {
    print "Database NOT Found ";
    mysql_close($db_handle);
}

?>
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208