gpt4 book ai didi

php - 将查询结果数组保存在 php 中

转载 作者:行者123 更新时间:2023-11-29 04:39:48 25 4
gpt4 key购买 nike

我正在执行一个查询,由此我从某个列 = 'xx' 的数据库表中检索所有行。

然后我需要存储从结果中检索到的数组。

然后我需要从数据库中删除所有这些行,但我仍然需要在执行删除后访问存储的数组。这听起来可能有点奇怪,但这是我需要做的。

到目前为止:

$Get_Appointments = "SELECT *
FROM TempSchedule2
WHERE CreatedBy = 'xx'";

$stmt = mysqli_stmt_init($con);
mysqli_stmt_prepare($stmt, $Get_Appointments);
mysqli_stmt_execute($stmt);
if ($Apts = mysqli_stmt_get_result($stmt)){
$numRows = mysqli_num_rows($Apts);
echo 'numrows is '.$numRows.'<br>';
global $arrayToSave;
$arrayToSave = mysqli_fetch_array($Apts);
while($row=mysqli_fetch_array($Apts)){
//echo 'in the while?';
$scheduleID = $row['scheduleID'];
$DeleteApt_Query = "DELETE FROM tempschedule2 WHERE scheduleID = ?";
$stmt = mysqli_stmt_init($con);
mysqli_stmt_prepare($stmt, $DeleteApt_Query);
mysqli_stmt_bind_param($stmt, "i", $scheduleID);

if (mysqli_stmt_execute($stmt)){
//executed
echo 'deleted';

}
else {
echo 'not deleted because '.mysqli_error($con);
$ErrorForLog = date("Y-m-d") . "\r\n" . mysqli_error($con). "\r\nInfo Attempted to delete apt id : " . $scheduleID. "\r\n\r\n";
error_log($ErrorForLog, 3, "Logs.txt");
}

}

echo 'savedArray here is ';
var_dump($arrayToSave);

发生的事情是由于某种原因没有删除一行。然后在执行删除查询的循环之后,我正在 var_dumping 全局 $arrayToSave(我希望它是整个数组),结果是数组中的一个元素没有被删除。所以似乎认为全局数组正在循环内被修改?这是我第一次不得不在 php 中使用 global,所以我可能没有正确使用它。

最佳答案

试试这个:

$arrayToSave = mysqli_fetch_array($Apts); 更改为 $arrayToSave = array();

然后在 while 循环开始后直接添加 $arrayToSave[] = $row

关于php - 将查询结果数组保存在 php 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252874/

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