Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Warning: mysql_num_rows(): supplied argument Error

    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. 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:

    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.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You need to assign the query result to a variable and use that in mysql_num_rows():
    PHP Code:
    $result mysql_query($update) or die(mysql_error());
    $num_rows mysql_num_rows($result); 

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,107
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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/functio...ected-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.
    Last edited by CFMaBiSmAd; 11-11-2007 at 02:14 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I didn't think that mysql_affected_rows() was inside the loop, I also think that $i already contains the number of rows

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by psykx View Post
    I didn't think that mysql_affected_rows() was inside the loop
    It's not, and that's not what CFMaBiSmAd said.
    Quote Originally Posted by CFMaBiSmAd
    Just putting one call to mysql_affected_rows() after the loop would only tell you if the last UPDATE changed anything.
    Quote Originally Posted by psykx View Post
    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.

  • #6
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    that will be not reading the writing properly again

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •