作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在执行一个查询,由此我从某个列 = '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/
我是一名优秀的程序员,十分优秀!