Jump to content

[SOLVED] not sure about how mysql_affected_rows works


Recommended Posts

So here is the query:

 

UPDATE images SET `title`='the line', `description`='this is a show made for TV', `src`='video/broadcast/the_line.flv' , `order`=1 WHERE ID=1

 

here is the PHP code:

 

$result = mysql_query($queryString) or die('update failed : '.$queryString);
$num_result = mysql_affected_rows($result);

 

here is the error:

 

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in...

 

now here is my question:

 

if the query fails then shouldn't die be called and then mysql_affected_rows could never be called to return the error above? so let say that the query is correct but it returns nothing and it effects nothing in the database. Is it possible that because no rows were effected it would produce this error. I thought it should just return a zero but $num_result actually equals nothing if I trace it. whatcha think?

 

 

Your thinking is correct. If the query failed die() should be called and script execution halted. I'm not sure how you could get the current error.

 

You could try some reorganising, but really, you should'nt need to.

 

<?php

  if (mysql_query($queryString)) {
    $num_result = mysql_affected_rows($result);
  } else {
    die('update failed : ' . $queryString);
  }

?>

Hi mysql_query() only returns a valid resource id for sql statements that product a result set (SELECT, etc.) Since an Insert statement does not produce a result set, the function returns bool true on success and bool false on error. So the result is correct. You should not try to use the result set as a resource id here. Instead use a resource id obtained from your connection or db selection.

 

Hope this helps :-)

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.