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.

图片转代码服务由CSDN问答提供 功能建议

我正在进行查询,我正在从db表中检索某个列='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'number'是'。​​$ numRows。'&lt; br&gt;'; \  n global $ arrayToSave; 
 $ arrayToSave = mysqli_fetch_array($ Apts); 
 while($ row = mysqli_fetch_array($ Apts)){
 // echo'in 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)){
 //执行
 echo'已删除'; 
 
} 
其他{\  n echo'未删除,因为'.mysqli_error($ con); 
 $ ErrorForLog = date(“Ymd”)。  “
 
”。  mysqli_error($ CON)。  “
 
Info尝试删除apt id:”。  $ scheduleID。 为 “
 
 
 
” 个;  
 error_log($ ErrorForLog,3,“Logs.txt”); 
} 
 
} 
 
回声'savedArray这里是'; 
 var_dump($ arrayToSave); 
  <  / pre> 
 
 

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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条)

相关推荐 更多相似问题