After executing the following code, $i
doesn't echo if I have the $q3
delete query enabled. If I comment it or delete $q3
, then everything is fine.
How can I get this to work?
$isfound = in_array("empty",$currentrooms0);
if($isfound===true){
for($i=0; $i<count($currentrooms0);$i++){
if($currentrooms0[$i]=="empty"){
$q3=mysqli_query($conn0,"delete from room_attributes where _current_created_rooms='empty' order by id desc limit 1")or die(mysqli_error($conn0));
echo $i-1;
break;
}
}
The reason it won't echo is because there is an error in your SQL and mysqli_query
returns false in such case.
The SQL causes an error because you cannot use ORDER BY
inside a WHERE
clause that directly belongs to a DELETE
If what you are looking to delete the topmost attribute then you might want to consider using this sql and your code should work.
DELETE FROM room_attributes
WHERE id IN (
SELECT id FROM room_attributes
WHERE _current_created_rooms='empty'
ORDER BY id DESC
LIMIT 1
)
I think you have problem with mysqli_connect please try to do following ..
if($q3 ===TRUE){
echo $i-1;
break;
}else{
echo "Connection Problem";
}
The OP wrote:
here is the answer:
$isfound = in_array("empty",$currentrooms0); if($isfound===true){ for($i=0; $i<count($currentrooms0);$i++){ if($currentrooms0[$i]=="empty"){ $q3=mysqli_query($conn0,"delete from room_attributes where _current_created_rooms='empty' order by id desc limit 1") or die(mysqli_error($conn0)); if(i>$i-1) {echo 0;}else{echo $i-1;} break; } }