ballouta Posted July 28, 2009 Share Posted July 28, 2009 Hello is there any code that displays for me if an update query was sucessfully done so display the appropriate message? if query update is fine echo "update working"; else echo "not working"; Thanks alot Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted July 28, 2009 Share Posted July 28, 2009 http://us2.php.net/manual/en/function.mysql-affected-rows.php if it is > 0 rows were updated Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 28, 2009 Share Posted July 28, 2009 mysql_affected_rows() tells you exactly that - if any rows were affected/updated. But, if the value is 0 that does not necessarily mean the query failed. The update query could have parameters such that the results are zero. For example, you may have a function to soft delete records that are x days old. If you ran it once and then try and run it again, the results of affected rows will be zero, but it did run successfully. So, use mysql_affected_rows() if you need to know if any records were uodated, but to see if the query ran succesfully you just need to test the query. Either this if (mysql_query($updateQuery)) { //success } else { //failure } Or this: $result = mysql_query($updateQuery; if ($result) { //success } else { //failure } Quote Link to comment Share on other sites More sharing options...
ballouta Posted July 29, 2009 Author Share Posted July 29, 2009 Thank you what if I want to know if a query returned any result or not? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 29, 2009 Share Posted July 29, 2009 Well an UPDATE query won't return any results. For a SELECT query you would do a test of mysql_num_rows(). Example: //Prepare the query $query = "SELECT field1, field2, filed3 FROM tableName WHERE field1 = 5"; //$run the query, assigning to a variable $result = mysql_query($query); //Validate that query ran if(!$result) { //Query failed, add error handling } else { //Check that there were results if (mysql_num_rows($result)==0) { //There were no results } else { //There were results, process the result set } } Quote Link to comment Share on other sites More sharing options...
ballouta Posted July 31, 2009 Author Share Posted July 31, 2009 Thank you so much Quote Link to comment 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.