Jump to content

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


cornelombaard

Recommended Posts

The following code gives me the error at the bottom of the page. What's wrong and how do I fix it? The book does not explain properly. Does anyone know what is the best book for learning PHP MySQL and Javascript for the web?

 

The code:

 

<html>

<head>

<title>

</title>

</head>

<body>

<pre>

<?php

$connection = mysql_connect("localhost", "corne", "c");

 

mysql_select_db("winestore", $connection);

 

$result = mysql_query("select winery_name, phone, fax from winery limit 20", $connection);

 

while($row = mysql_fetch_array($result))

{

print "The {$row["Winery Name"]} Winery's fax is {$row["fax"]} Their phone is {$row["phone"]}. \n";

}

?>

</pre>

</body>

</html>

 

 

The Error:

 

Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/testphp/database.php on line 15

 

Also what is MYSQ_NUM?

The error means that your mysql_query() failed and it returned a FALSE value instead of a result resource. See the section in the php manual for a complete description of mysql_query

mysql_query can fail either due to a problem with your php/mysql configuration, a problem with the mysql server, a problem with the selected database, or due to a syntax error in your query. To get php/mysql to tell you why it failed use some error checking logic.

 

For the first possibility (php/mysql configuration) add the following two lines of code immediately after your first opening <?php tag -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

For the rest of the possible reasons add an or die(mysql_error()); statement to the end of your mysql_connect, mysql_select_db, and mysql_query() statements -

 

$connection = mysql_connect("localhost", "corne", "c") or die('Could not connect to database server ' . mysql_error());

mysql_select_db("winestore", $connection) or die('Could not select database ' . mysql_error());

$result = mysql_query("select winery_name, phone, fax from winery limit 20", $connection) or die('Query failed ' . mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.