i got strange behavior cannot allows me delete single line on table, in fact, enters mysqli->query line ask delete, stops , nothing (the echo post after isn't displayed @ all), here code, , i'd know what's wrong :
i've posted in deleteline function thing outpouts correctly echo, , things don't :
<?php //here db connexion include 'conndb.php'; //array that'll stock values delete $myarray = array(); //lists elements of table animals if ($result = $mysqli->query("select * animals")) { while($row = $result->fetch_array(mysql_assoc)) { //searching specific lines $delay= timepassed($row["time"]); if($delay >= 60){ //save id of line in array $myarray[] = $row["id"]; } } } $result->close(); //once lines delete saved in array, let's delete for($i=0;$i<count($myarray);$i++){ //will call , delete 1 line 1 deleteline($myarray[$i]); } $mysqli->close(); ////////////////////////////////////////////////////////////////////////////////////////////////timepassed //just compare 2 timestamps between them function timepassed($datetocompare){ $datenow = time(); $res =round(($datenow-$datetocompare)/(60*60)); return $res; } ////////////////////////////////////////////////////////////////////////////////////////////////deleteline function deleteline($id){ echo $id; //outputs correctly id of line targeted (in case : 83) $mysqli->query("delete animals id=".$id); echo"line deleted"; //this never outputs } ?>
i don't see how database connection object $mysqli
. inside function, put global $mysqli
use connection object @ outside function.
function deleteline($id){ global $mysqli;//database connection object $mysqli->query("delete `animals` `id` ='$id'"); if($mysqli->affected_rows>0){ echo "line deleted"; }else{ echo 'delete failed'; } $mysqli->close(); }
hope may , use prepared statement better security.
Comments
Post a Comment