mysql_fetch_assoc(): supplied argument is not a valid MySQL

Problems with the Windows version of XAMPP, questions, comments, and anything related.

mysql_fetch_assoc(): supplied argument is not a valid MySQL

Postby oculus » 12. May 2009 11:34

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
oculus
 
Posts: 18
Joined: 11. May 2009 10:16

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

Postby oculus » 12. May 2009 11:48

Oh, I've just resolved this.
oculus
 
Posts: 18
Joined: 11. May 2009 10:16

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

Postby Wiedmann » 12. May 2009 11:50

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".
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

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

Postby oculus » 12. May 2009 11:55

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.
oculus
 
Posts: 18
Joined: 11. May 2009 10:16


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 54 guests