Jump to content

mysql_fetch_array() error issues


oskom

Recommended Posts

Hello all,

I've been surfing forum threads on this, and there are plenty, with no satisfactory results. Here's the deal...

I've got quite a few instances of this very common query code on my site:

//example 1
$result = mysql_query("SELECT * FROM table WHERE ID = ".$_REQUEST['ID']."");
$row = mysql_fetch_array($result); //LINE OF CODE IN ERROR

//example 2
$result = mysql_query("SELECT * FROM table WHERE ID = ".$_REQUEST['ID']."");
while($row = mysql_fetch_array($result)) { //LINE OF CODE IN ERROR
//LOOP DATA
}

The PHP error log will frequently give a warning to this effect:"PHP Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/local/ftp/path/to/file.php on line 15".

The important thing to know is that I ALWAYS get the desired result from the database. I've added "or die(mysql_error())" to the end of the queries with no error being displayed and echoing the $result will always give me the resource id number.

 

If I'm getting the data, why would there be a warning? Most of the diagnoses of this error in these forum threads I've been reading keep mentioning bad queries or database connections. Apparently, neither of those things are the problem if I'm actually getting results.

 

Enlightenment?

Link to comment
Share on other sites

Replace your examples with:

<?php
//example 1
$q = "SELECT * FROM table WHERE ID = ".$_REQUEST['ID'];
$result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
$row = mysql_fetch_array($result); //LINE OF CODE IN ERROR

//example 2
$q = "SELECT * FROM table WHERE ID = ".$_REQUEST['ID'];
$result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
while($row = mysql_fetch_array($result)) { //LINE OF CODE IN ERROR
//LOOP DATA
}?>

 

This should tell you what the problem is.

 

Ken

Link to comment
Share on other sites

I can only reassert: I've already done all of those things already!!!!

 

Rewriting the code(as I have just done to test it) like this...

$result = mysql_query("SELECT * FROM table WHERE ID = ".$_REQUEST['ID']."")  or die("Problem with the query: $q<br>" . mysql_error());
$row = mysql_fetch_array($result); //LINE OF CODE IN ERROR

...will display no errors on the page itself. I get the data I wanted just fine. Whether or not the "die()" statement exists after the query seems to be irrelevant, which says to me that the query is fine. The more telling thing is that I'm actually GETTING DATA! MySQL likes my query enough to give me the data but PHP gets all persnickety about something.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.