| re: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
"Noel Wood" <removeuptohereelwood@optusnet.com.au> wrote in message
news:4136f1f5$0$10824$afc38c87@news.optusnet.com.a u...[color=blue]
> Hello I keep getting a warning ...
> Warning: mysql_fetch_array(): supplied argument is not a valid MySQL[/color]
result[color=blue]
> resource in "name of my php file" when I run this query....
> SELECT Menu_Item, Quantity
> FROM restorder_item
> WHERE Order_Number =58
> The code I use to do it is....
> $query = "select Menu_Item, Quantity from restorder_item";
> $query .= " where Order_Number = ".$_REQUEST["OrderNumber"];
>
> $result = mysql_query($query) or die($query." failed: ".mysql_error());
>
> while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
> var_dump($row);
> }
> Notice to test it I did a var_dump of $row and it came back with the
> expected results namely......
> array(2) { ["Menu_Item"]=> string(1) "3" ["Quantity"]=> string(1) "5" }
> So I can't see whats wrong. Everything seems to be working yet I get a
> warning.
> I have done a quick fix by putting an @ in front of the offending function
> but I would really like to know what the problem is.
> Thanks for your help in advance.[/color]
Your testing for errors leaves a lot to be disired. You only check to see if
mysql.error is set and don't check to see if a results set has been
returned, or that result set actually contains rows.
$result = mysql_query($query);
if(! $result || mysql_error() || mysql_num_rows($result) < 1)
{
echo sprintf('%s rows, [%s] , error: %s',
@mysql_num_rows($result),
$query,
mysql_error());
exit;
}
Note the @ on the front of the second mysql_num_rows. If the result set is
empty then trying to use mysql_num_rows will generate the error that you are
seeing, so you should suppress it. |