Earthenware Posted January 6, 2012 Share Posted January 6, 2012 Hi, I have a feeling that this is an obvious one but I can’t seem to find an answer. I am trying to evaluate the result of an UPDATE query. My syntax works fine and my DB gets updated as expected. My problem is that if I put a non-existent value in my WHERE clause, I don’t get warned about it. I’m doing this: error_reporting(E_ALL); $query = "update <TABLE> set <FIELD1> = '<VALUE1>' where <FIELD2> = '<VALUE2>'"; $result = mysql_query ($query, $db) or die ("Query failed: " . mysql_error() . " Actual query: " . $query); The result is the same whether VALUE2 exists in the DB or not. $result is always returned as ‘1’. According to dev.mysql.com, “UPDATE returns the number of rows that were actually changed”, but I get ‘1’ whether a row was changed or not. My goal of course is to have the script notify me if the record that I am trying to update doesn’t exist. That is what is not happening at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/254475-checking-result-of-mysql-update-query/ Share on other sites More sharing options...
trq Posted January 6, 2012 Share Posted January 6, 2012 mysql_query() returns true or false when executing queries that don't return a result resource. If you try an update a record that doesn't exist using mysql_query() you will egt an error. Quote Link to comment https://forums.phpfreaks.com/topic/254475-checking-result-of-mysql-update-query/#findComment-1304796 Share on other sites More sharing options...
Earthenware Posted January 6, 2012 Author Share Posted January 6, 2012 That's my problem. I'm not getting any error. The output is the same whether the record exists or not. Quote Link to comment https://forums.phpfreaks.com/topic/254475-checking-result-of-mysql-update-query/#findComment-1304798 Share on other sites More sharing options...
trq Posted January 6, 2012 Share Posted January 6, 2012 You can check mysql_affected_rows to see if any rows where updated. Quote Link to comment https://forums.phpfreaks.com/topic/254475-checking-result-of-mysql-update-query/#findComment-1304799 Share on other sites More sharing options...
Earthenware Posted January 6, 2012 Author Share Posted January 6, 2012 That worked. I've put... $rc = mysql_affected_rows(); ...below the code above and it returns the expected value, so I can script around that. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/254475-checking-result-of-mysql-update-query/#findComment-1304804 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.