将查询结果数组保存在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.

Try this:

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

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

You fetch one row before the loop thereby removing it from the results $Apts:

$arrayToSave = mysqli_fetch_array($Apts);

So $arrayToSave only contains one row and that row is not looped over. You want to remove that and probably do something like this:

while($row=mysqli_fetch_array($Apts)){
    $arrayToSave[] = $row;

Alternately use mysqli_fetch_all():

$arrayToSave = mysqli_fetch_all(($Apts);
foreach($arrayToSave as $row) {
    //do stuff
}

mysqli_fetch_array grabs the next row and turns it into an array. It doesn't take the whole result set and turn it into an array. That is why you put it in a while statement. That loops through ALL the rows. So loop through and save the results, then run your delete afterward.

$results = array();
while($row = mysqli_fetch_array($apts){
    $results[] = $row;
}
//Now Delete
$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");
}

Also, you need to be careful running another query while you are still iterating through the result of a previous query. The data from the first query gets wiped out when you run another query.

global is useless in this place, you can safely delete it.

What you do is put the first retrieved row into $arrayToSave and iterate the over the rest of rows. What you probably want to do, is put all $rows into a two-dimensional array with something like $arrayToSave[] = $row; after the beginning of the while loop.