mysql fetch variable,PHP-多个while($ row = mysql_fetch_array($ variable)){}错误

Okay my syntax can probably be described without the code. Basically it should be easy...but never is.

I have 2 loops in a row...basically the same thing. I SELECT * every variable from my database, and then I need to build a 2 layer javascript based on two sep. variables.

So

I have:

while ($row = mysql_fetch_array($myVariable)) {

// do events

}

then after that

while ($row2 = mysql_fetch_array($myVariable)) {

// do events

}

For some reason it's completely returning NOTHING on the second one...is there some where I need to reset my array, did it possibly end and then I can't just restart. Such a basic question, but I can't figure out what else could be wrong for the life of me. So thats the question, is that a general issue to just do that twice in a row and expect PHP to start over each time at the beginning of $myVariable which holds my SELECT statement?

Thanks...will check all day and update as needed. First timer to Stack Overflow i'd love to see the help.

解决方案

The problem is that the query returns a resource. This resource has it's own internal pointer (counter to remember which row to return). mysql_fetch_array will advance that internal pointer and return each row from the resource one at a time. once it is at the end (no more rows left), it returns false. so on the last loop, $row equals false which exits the while loop. The pointer is not reset though. So on the next while loop, you call mysql_fetch_array, the pointer is at the end and so it returns false which completely bypasses the second while loop. What you should do, is loop through the data once and store each row into an array. Then you can copy/loop over the array as much as you want and everything works as expected.

$data = array();

while ($row = mysql_fetch_array($myVariable)) {

$data[] = $row;

}

//now $data has all the rows. a simple foreach will loop over the data.

foreach($data as $row){

//do events

print_r($row);

}

//and you can loop over data again and again.

foreach($data as $row){

//do events

print_r($row);

}

这段代码有一个错误,即在执行 UPDATE SQL 语句之后,使用 `mysqli_fetch_array()` 函数尝试获取查询结果。由于 UPDATE 语句并不会返回查询结果,因此调用 `mysqli_fetch_array()` 函数时会出现上述错误。 如果你只是想检查 UPDATE 语句是否执行成功,可以使用 `mysqli_affected_rows()` 函数来获取受影响的行数,例如: ```php public function updateSeat($s_h_id, $seats) { $db = new DbManage(); $sqlTxt = "UPDATE screening_hall_info SET seats = '".$seats."' WHERE s_h_id = ".$s_h_id; $result = $db->executeSqlTxt($sqlTxt); $affectedRows = mysqli_affected_rows($db->getConnection()); return $affectedRows; } ``` 在这个例子中,我们使用 `mysqli_affected_rows()` 函数获取受影响的行数,并将结果返回。如果受影响的行数大于 0,说明 UPDATE 语句执行成功。如果受影响的行数等于 0,说明 UPDATE 语句没有修改任何数据。如果受影响的行数小于 0,说明执行 UPDATE 语句时发生了错误。 如果你需要获取更新后的数据,你可以在 UPDATE 语句执行之前,先使用 SELECT 语句查询一下原始数据,例如: ```php public function updateSeat($s_h_id, $seats) { $db = new DbManage(); // 查询原始数据 $query = "SELECT * FROM screening_hall_info WHERE s_h_id = ".$s_h_id; $result = $db->executeSqlTxt($query); $oldData = mysqli_fetch_array($result); // 更新数据 $sqlTxt = "UPDATE screening_hall_info SET seats = '".$seats."' WHERE s_h_id = ".$s_h_id; $result = $db->executeSqlTxt($sqlTxt); // 查询更新后的数据 $query = "SELECT * FROM screening_hall_info WHERE s_h_id = ".$s_h_id; $result = $db->executeSqlTxt($query); $newData = mysqli_fetch_array($result); return array("old" => $oldData, "new" => $newData); } ``` 在这个例子中,我们先使用 SELECT 语句查询原始数据,并使用 `mysqli_fetch_array()` 函数将查询结果存储在 `$oldData` 变量中。然后执行 UPDATE 语句,更新数据。最后再使用 SELECT 语句查询更新后的数据,并将查询结果存储在 `$newData` 变量中。最终,我们将原始数据和更新后的数据都返回给调用者,以便进行比较。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值