Page 1 of 1

mysql_fetch_assoc(): supplied argument is not a valid MySQL

PostPosted: 12. May 2009 11:34
by oculus
I'm trying to see the output of the data from the database named "addressbook."
Here's the script:
Code: Select all
<?PHP

$user_name = "root";
$password = "mypass";
$database = "addressbook";
$server = "localhost";

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

?>


But what's wrong with that? Below shows when I view it on my browser:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\myphp02\index.php on line 15

Re: mysql_fetch_assoc(): supplied argument is not a valid MySQL

PostPosted: 12. May 2009 11:48
by oculus
Oh, I've just resolved this.

Re: mysql_fetch_assoc(): supplied argument is not a valid MySQL

PostPosted: 12. May 2009 11:50
by Wiedmann
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\myphp02\index.php on line 15

argument for mysql_fetch_assoc() is the result from mysql_query(). So this query fails. Like you can read in the PHP manual, you should test if the query was successful.

BTW:
For posting code there is a "code" tag, not "color".

Re: mysql_fetch_assoc(): supplied argument is not a valid MySQL

PostPosted: 12. May 2009 11:55
by oculus
Oh, am sorry... I'm gonna use it the next time I post my query here. ;-)

Anyway, I've added this code:

Code: Select all
or die(mysql_error());
after
Code: Select all
$result = mysql_query($SQL);
so I've learned that I couldn't connect to the database due to the wrong database name. It was tbl_address_book NOT tb_address_book.