drj26159 2015-08-27 14:59
浏览 95
已采纳

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

I am conducting a query whereby I am retrieving all rows from a db table where a certain column = 'xx'.

Then I need to store the array retrieved from the result.

Then I need to delete all those rows from the DB, but i still need to access the stored array AFTER carrying out the delete. It might sound a bit strange but this is what I need to do.

So far:

$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") . "
" . mysqli_error($con). "
Info Attempted to delete apt id : " . $scheduleID. "

"; 
            error_log($ErrorForLog, 3, "Logs.txt");
        }

    }

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

What's happening is one row is not deleting for some reason. And then after the loop that executes the delete queries, I am var_dumping the global $arrayToSave (which i hoped was the entire array), and the result is that one element of the array which had not been deleted. So it seems as thought the global array is being modified within the loop? This is the first time I've had to use global in php so I'm probably not using it right.

  • 写回答

4条回答 默认 最新

  • doubi1797 2015-08-27 15:05
    关注

    Try this:

    Change $arrayToSave = mysqli_fetch_array($Apts); to $arrayToSave = array();

    Then add $arrayToSave[] = $row directly after the start of the while loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?