theagonizer Posted November 11, 2007 Share Posted November 11, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/76826-solved-not-sure-about-how-mysql_affected_rows-works/ Share on other sites More sharing options...
trq Posted November 11, 2007 Share Posted November 11, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/76826-solved-not-sure-about-how-mysql_affected_rows-works/#findComment-388961 Share on other sites More sharing options...
Monotoba Posted November 11, 2007 Share Posted November 11, 2007 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/76826-solved-not-sure-about-how-mysql_affected_rows-works/#findComment-389297 Share on other sites More sharing options...
theagonizer Posted November 11, 2007 Author Share Posted November 11, 2007 I solved the problem by getting around the statement calling an error but it sure helps to know exactly why the error was occurring. You guys are freekn awesome! Quote Link to comment https://forums.phpfreaks.com/topic/76826-solved-not-sure-about-how-mysql_affected_rows-works/#findComment-389398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.