如何在同一PHP页面上将mysqli fetch_assoc()与已准备好的语句一起使用几次? [英] How can I use mysqli fetch_assoc() several times with prepared statements on same PHP page?
问题描述
有没有一种方法可以通过准备好的语句在同一页面上多次使用 fetch_assoc()
?
Is there a way to enable the use of fetch_assoc()
several times on the same page with prepared statements?
$data = $conn->prepare("SELECT * FROM some_table WHERE id=?");
$data->bind_param('i',$id);
$data->execute();
$result = $data->get_result();
我希望能够在同一页面上使用 fetch_assoc()
多次,只要我想使用多种方式即可.如果我想循环两次,我应该能够做到.如果我想循环并执行一次访存,那么我也应该能够做到这一点.现在看来,一旦 fetch_assoc()
被使用一次,便无法在同一页面上再次使用.
I'd like to be able to use fetch_assoc()
on the same page as many times as I want as many different ways I want. If I want to loop twice, I should be able to do that. If I want to loop and do a single fetch, I should also be able to do that. Right now it seems, once fetch_assoc()
is being used once, it cannot be used again on the same page.
循环播放
while($row = $result->fetch_assoc()){
...
}
单次获取
$user = $result->fetch_assoc();
$first_name = $user['first_name'];
推荐答案
您可以使用 mysqli_result :: data_seek
会在每次使用后重置结果指针,以便您可以多次遍历结果.例如:
You can use mysqli_result::data_seek
to reset the result pointer after each usage so you can loop over the results multiple times. For example:
while($row = $result->fetch_assoc()){
...
}
$result->data_seek(0);
while($row = $result->fetch_assoc()){
...
}
这篇关于如何在同一PHP页面上将mysqli fetch_assoc()与已准备好的语句一起使用几次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!