Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_fetch_array():

Status
Not open for further replies.

gokeeffe

Programmer
Jan 11, 2005
170
IE
I keep getting the follwing error with this piece of code


----------------- Error ------------------------------
mysql_fetch_array(): supplied argument is not a valid MySQL result resource.


------------------ Code -------------------------------

$c1 = $_SESSION['subject_id'];

$query_result = mysql_query ("SELECT * FROM subjects WHERE subject_id = '$c1'");

while ($row = mysql_fetch_array ($query_result, MYSQL_NUM))

{
$cc1 = $row[2];
echo "<p>You have choosen to give grinds for the <font size='+1'> $cc1 </font></p>";
}



Tks
 
This is a PHP question, not a MySQL question. But I guess I'll answer as the question relates collaterally to MySQL.

When used with a SELECT query, mysql_query() can return either a resource handle (if the query is successful) or FALSE (if the query generated a MySQL error). If you attempt to pass FALSE to mysql_fetch_array(), you will get this error, as FALSE is not a valid resource handle.

Your code needs more error checking. At an absolute minimum, you need to change:

while ($row = mysql_fetch_array ($query_result, MYSQL_NUM))

{
$cc1 = $row[2];
echo "<p>You have choosen to give grinds for the <font size='+1'> $cc1 </font></p>";
}

To read:


if ($query_result !== FALSE)
{
while ($row = mysql_fetch_array ($query_result, MYSQL_NUM))
{
$cc1 = $row[2];
echo "<p>You have choosen to give grinds for the <font size='+1'> $cc1 </font></p>";
}
}
else
{
die (mysql_error());
}

this will give you the debugging information you need. For further debugging advice with PHP, see faq434-2999

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top