Jump to content

[SOLVED] not sure about how mysql_affected_rows works


theagonizer

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.