Jump to content

[SOLVED] mysqli_query DELETE returns TRUE whether or not there are records to delete


Recommended Posts

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
}

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.

 

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>";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.