gpt4 book ai didi

php - Mysqli 删除请求 block

转载 作者:搜寻专家 更新时间:2023-10-31 21:03:43 25 4
gpt4 key购买 nike

我有一个奇怪的行为,不允许我删除表中的一行,事实上,一旦它进入要求删除的 mysqli->query 行,它就会停止并且什么都不做(之后的回显它根本没有显示),这是代码,我想知道出了什么问题:

我已经在 deleteLine 函数中发布了使用 echo 正确输出的内容,以及不正确输出的内容:

<?php
//here are the db connexion
include 'connDB.php';
//array that'll stock my values to delete
$myarray = array();

//lists all elements of the Table animals
if ($result = $mysqli->query("SELECT * FROM animals")) {

while($row = $result->fetch_array(MYSQL_ASSOC)) {

//searching for some specific lines
$delay= timePassed($row["time"]);
if($delay >= 60){
//save the id of the line in an array
$myArray[] = $row["id"];
}
}

}
$result->close();

//Once all my lines to delete are saved in my array, let's delete it
for($i=0;$i<count($myArray);$i++){
//will call and delete one line by one
deleteLine($myArray[$i]);
}
$mysqli->close();


////////////////////////////////////////////////////////////////////////////////////////////////TIMEPASSED
//just to 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 THE ID OF THE LINE TARGETED (in my case : 83)
$mysqli->query("DELETE FROM animals WHERE id=".$id);
echo"line deleted"; //THIS NEVER OUTPUTS

}
?>

最佳答案

我不明白你是如何获得你的数据库连接对象 $mysqli 的。在函数内部,我放置了 global $mysqli 以使用函数外部的连接对象。

function deleteLine($id){
global $mysqli;//database connection object
$mysqli->query("DELETE FROM `animals` WHERE `id` ='$id'");
if($mysqli->affected_rows>0){
echo "line deleted";
}else{
echo 'delete failed';
}
$mysqli->close();
}

希望这可以帮助和使用 prepared statement以获得更好的安全性。

关于php - Mysqli 删除请求 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36237206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com