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.