I created a database from a turorial, but gets the error message:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\addressbook\MysqlStep3HandleReadRecord.php on line 16

Part of the code is:

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

?>

instead of

while ($db_field = mysql_fetch_assoc($result)) {

you can try some thing like this

while($db_field = mysql_fetch_array($sql, MYSQL_BOTH)){

you usually get that problem when $result isnt what the mysql_fetch_assoc() function expects. This usually means that somethin has gone wrong earlier.

if you change this:

$result = mysql_query($SQL);

to this:

$result = mysql_query($SQL) or die(mysql_error());

your page should output a more useful error to help you. Problems could be connecting to the database or a query error...

Member Avatar for diafol

does changing SELECT*FROM tb_address_book to SELECT * FROM tb_address_book help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.