lhbdan Posted March 14, 2011 Share Posted March 14, 2011 I have been trying to write code that deletes a book from a database if the user provides a BookName, and the corresponding DeletionCode. I then want to provide the user with feedback, telling the user whether they have deleted a book, that is they have provided a deletion code and a book name, or whether they have not deleted a book, because they have entered the wrong bookname or book ID. I thought this code would work, but it seems that the $result variable is always set as TRUE, no matter whether the code has deleted a row or not. What have i done wrong? $result = mysql_query("DELETE FROM books WHERE BookName = '$_POST[bookName]' && DeletionCode = '$_POST[Deletioncode]'"); if(!$result){ die('Invalid query: ' . mysql_error()); } else echo "delete book"; Quote Link to comment https://forums.phpfreaks.com/topic/230559-check-mysql_query-result-true-or-false/ Share on other sites More sharing options...
lhbdan Posted March 14, 2011 Author Share Posted March 14, 2011 sorry the last echo should say "deleted book"; not "delete book"; Quote Link to comment https://forums.phpfreaks.com/topic/230559-check-mysql_query-result-true-or-false/#findComment-1187203 Share on other sites More sharing options...
DavidAM Posted March 14, 2011 Share Posted March 14, 2011 mysql_query() will only return false if the query fails - i.e. if it is invalid. A DELETE (or UPDATE) statement that does not affect any rows, is valid. Use mysql_affected_rows() to see how many rows were deleted. Quote Link to comment https://forums.phpfreaks.com/topic/230559-check-mysql_query-result-true-or-false/#findComment-1187204 Share on other sites More sharing options...
lhbdan Posted March 14, 2011 Author Share Posted March 14, 2011 thanks, so how will that fit into my code? Quote Link to comment https://forums.phpfreaks.com/topic/230559-check-mysql_query-result-true-or-false/#findComment-1187205 Share on other sites More sharing options...
DavidAM Posted March 14, 2011 Share Posted March 14, 2011 Something like this if (! $result) { // Query Failed } elseif (mysql_affected_rows() == 0) { // No such book } else { // Book Deleted } Quote Link to comment https://forums.phpfreaks.com/topic/230559-check-mysql_query-result-true-or-false/#findComment-1187206 Share on other sites More sharing options...
lhbdan Posted March 14, 2011 Author Share Posted March 14, 2011 thankyou!!! Quote Link to comment https://forums.phpfreaks.com/topic/230559-check-mysql_query-result-true-or-false/#findComment-1187207 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.