For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2006 > mysql_fetch_row issue. Please read.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author mysql_fetch_row issue. Please read.
sectionFOURTEEN

2006-03-29, 6:57 pm

Here's a snippet of my script:

$result = mysql_query("SELECT count(*) FROM cart WHERE
cookieId = '" . GetCartId() . "' AND prod_id = $prod_id"); //line 39
$row = mysql_fetch_row($result);
$numRows = $row[0];

I'm getting the "supplied argument is not a valid MySQL result resource
on line 39" error. I know it's getting returned because there aren't
any records that match this query. However, I would like it to return
zero records instead of this error. I have the rest of the script setup
to handle zero records returned but, this error is being returned
instead. This script works when there are records to return.

I was thinking about an if statement possibly but, I'm not sure which
direction to go with this.

Any help would be greatly appreciated!

Colin McKinnon

2006-03-29, 6:57 pm

sectionFOURTEEN wrote:

> Here's a snippet of my script:
>
> $result = mysql_query("SELECT count(*) FROM cart WHERE
> cookieId = '" . GetCartId() . "' AND prod_id = $prod_id"); //line 39
> $row = mysql_fetch_row($result);
> $numRows = $row[0];
>
> I'm getting the "supplied argument is not a valid MySQL result resource
> on line 39" error. I know it's getting returned because there aren't
> any records that match this query. However, I would like it to return
> zero records instead of this error. I have the rest of the script setup
> to handle zero records returned but, this error is being returned
> instead. This script works when there are records to return.
>


Off the top of my head...

$numRows = (integer)$row[0];

?

C.


diogo86@gmail.com

2006-03-29, 6:57 pm

this error message doesn't seem to be about no records found but a sql
error on the query.

try this:

$res = mysql_query("SELECT count(*) FROM cart WHERE
cookieId = '" . GetCartId() . "' AND prod_id = $prod_id") or
die(mysql_error());

$numRows = mysql_num_rows($res) ? mysql_result($res,0) : 0;

mysql_free_result($res);

when you correct the query and it's no longer returning an error you
show remove " or die(mysql_error())" to avoid relevant data to be show
to the user just in case it has an error.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com