FeralReason Posted November 6, 2009 Share Posted November 6, 2009 Just put together a delete order function (which seems to delete orders appropriately) but I am noticing that, when using the mysqli_query function with a DELETE query, it returns TRUE whether or not records are found to delete. Is this what I should expect ? $orderid = $_GET ['var']; if($orderid){ $mysqli=getDBconnection(); // START DB activity $delete_qry = "DELETE FROM orders WHERE orderid = '".$orderid."'"; $result = mysqli_query($mysqli, $delete_qry) or die(mysqli_error($mysqli)); if($result==TRUE){ echo "ORDER ".$orderid." DELETED.<br>"; } $delete_qry = "DELETE FROM order_items WHERE orderid = '".$orderid."'"; $result = mysqli_query($mysqli, $delete_qry) or die(mysqli_error($mysqli)); if($result==TRUE){ echo " ORDER ITEMS(s) for ".$orderid." DELETED.<br>"; } mysqli_close($mysqli); // close connection to MySQL } Quote Link to comment https://forums.phpfreaks.com/topic/180567-solved-mysqli_query-delete-returns-true-whether-or-not-there-are-records-to-delete/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2009 Share Posted November 6, 2009 Is this what I should expect ? Yes, a TRUE value returned by mysqli_query() just means that the query was execuited - Return Values Returns TRUE on success or FALSE on failure. For SELECT, SHOW, DESCRIBE or EXPLAIN mysqli_query() will return a result object. You must use mysqli_affected_rows() to find out the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query. Quote Link to comment https://forums.phpfreaks.com/topic/180567-solved-mysqli_query-delete-returns-true-whether-or-not-there-are-records-to-delete/#findComment-952640 Share on other sites More sharing options...
FeralReason Posted November 6, 2009 Author Share Posted November 6, 2009 Thanks much. That clarifies it for me. For the sake of others, here's the final code: $orderid = $_GET ['var']; if($orderid){ $mysqli=getDBconnection(); // START DB activity (my function) $delete_qry = "DELETE FROM orders WHERE orderid = '".$orderid."'"; $result = mysqli_query($mysqli, $delete_qry) // Note using mysqli_query -- not mysql_query or die(mysqli_error($mysqli)); if($result){ $count = mysqli_affected_rows($mysqli); } if($count>0){ echo "ORDER ".$orderid." DELETED.<br>"; } else{ echo "ORDER ".$orderid." not found.<br>"; } $delete_qry = "DELETE FROM order_items WHERE orderid = '".$orderid."'"; $result = mysqli_query($mysqli, $delete_qry) // mysqli_query returns TRUE on success or die(mysqli_error($mysqli)); if($result){ $count = mysqli_affected_rows($mysqli); } if($count>0){ echo $count."ORDER ITEMS(s) for ".$orderid." DELETED.<br>"; } else{ echo "No ORDER ITEMS for order ".$orderid." found.<br>"; } mysqli_close($mysqli); // close connection to MySQL } else{ echo "NO RECORD SELECTED. PLEASE SELECT A RECORD.<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/180567-solved-mysqli_query-delete-returns-true-whether-or-not-there-are-records-to-delete/#findComment-952670 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.