...

View Full Version : Warning: mysql_num_rows(): supplied argument Error



Skormy
11-11-2007, 06:58 AM
I'm receiving the error above in my shopping cart application. I sell one-off items and use Paypal as the payment engine. In the remote possibility an item was previously purchased I loop through each order and update one at a time. I'm counting rows so if a record is not updated, I'd like to know. Strangely, it's working as designed (sending emai), but don't want to deploy unless I know why this error is occuring.

Here's what I've got so far. Here's the full error:

1. :confused: is not a valid MySQL result resource in cartworks15.php on line 105.

2. Do I need to change the variable to a different function. Is my syntax correct.

Please let me know. Thanks in advance.

3. The code:



mysql_query ($query);

// breaks the string into an array of products
$products = explode("~", $cart);

$i = 0;

// breaks each product string into its own array
foreach($products as $var => $val){

$product[$i] = explode(":", $val);
$desc = $product[$i][0];
$qty = $product[$i][1];
$money = $product[$i][2];
$pid = $product[$i][4];

$update = "UPDATE ckd_labinv SET status ='S',
sold_code = 'COD', sold_date = NOW()
WHERE pid = '$pid'
and status = 'A'";
mysql_query($update) or die(mysql_error());

$i++;
}

//echo '...Updated Database <br>';

$to = "me@test.com";
$subject = "Already Purchased Alert!";
$contents = "\nThe following item has already been purchased:\n\n * $desc \n\nThis item was purchased by the following customer:\n\n $inv_name,$email, on $date.\n";
$from_header = "From: purchasealerts@ckd.com";
$num_rows = mysql_num_rows($update); //offending line
//mysql_affected_rows($update);

if ($num_rows == 0)
{
mail( $to, $subject, $contents, $from_header);
echo '...sent Already Purchased Alert <br>';
}




Thanks in advance.

Inigoesdr
11-11-2007, 07:26 AM
You need to assign the query result to a variable and use that in mysql_num_rows():

$result = mysql_query($update) or die(mysql_error());
$num_rows = mysql_num_rows($result);

CFMaBiSmAd
11-11-2007, 03:11 PM
This is a case where reading the php manual concerning what you are attempting to do would have told you what you should be doing - http://php.net/mysql_query

You are executing an UPDATE query. From the description of mysql_query() at the above link -

Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning result set, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement. and if you are going to use mysql_affected_rows(), please read the description of it in the php manual, because the optional parameter, is not a result resource returned by an UPDATE query - http://www.php.net/manual/en/function.mysql-affected-rows.php

Also, because you are using the UPDATE query inside of a loop, you will need to accumulate the number of affected rows for all the queries. Just putting one call to mysql_affected_rows() after the loop would only tell you if the last UPDATE changed anything.

psykx
11-11-2007, 05:16 PM
I didn't think that mysql_affected_rows() was inside the loop, I also think that $i already contains the number of rows

Inigoesdr
11-11-2007, 06:24 PM
I didn't think that mysql_affected_rows() was inside the loop
It's not, and that's not what CFMaBiSmAd said.

Just putting one call to mysql_affected_rows() after the loop would only tell you if the last UPDATE changed anything.


I also think that $i already contains the number of rows

That's true if you assume that each update query executes successfully. You only actually check to see if the last one did.

psykx
11-11-2007, 08:31 PM
that will be not reading the writing properly again :rolleyes:

and yes using $i don't include error checking again me not reading the writing properly again I thought it was mysqli_num_rows()



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum