重置MySqli指针? [英] Reset MySqli pointer?

查看:67
本文介绍了重置MySqli指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在重置指针方面有些挣扎.我想要这样做是因为我将在同一脚本中使用两次相同的查询.据我所知,我可以在遍历读取数组之后重置指针.如果有更好的方法可以做到这一点,我很想听听.

I'm struggling a bit with resetting a pointer. I want to do this because I'm going to use the same query twice in the same script. As far as I can work out I can do this with resetting the pointer after I've looped trough the fetched array. If there is a better way to do this, I'd love to hear it.

无论如何,这就是我所得到的.

Anyway, this is what I got.

$getEvent = $connection->prepare("SELECT * BLABLA FROM BLABLA");

$getEvent->bind_param("i", $eventID); 

    $getEvent->execute();
    $getEvent->bind_result($eventMember, $eventStaff, $eventMemberID, $eventMemberSuper);

while($getEvent->fetch())
    {
        // Do stuff
    }

// Then bunch of code, before finally another

//$getEvent->execute(); //Script doesn't work without this and next line
//$getEvent->bind_result($eventMember, $eventStaff, $eventMemberID, $eventMemberSuper);

 while($getEvent->fetch())
    {
      // Do other stuff
    }

我尝试过$getEvent->data_seek(0);,但是没有运气.该脚本仅在我重新声明$getEvent->bind_result时有效.预先感谢您的任何答复.

I've tried with $getEvent->data_seek(0); but no luck. The script only works if I redeclare $getEvent->bind_result. Thanks in advance for any replies.

推荐答案

这会对数据库服务器造成不必要的额外负担.与其倒带并重用结果集,不如将整个事情存储在一个数组中.然后,您可以在PHP应用程序代码中随意使用它多次,并且使用不同的方式.

This places unnecessary extra strain on the database server. Rather than rewind and reuse the result set, store the whole thing in an array to begin with. You may then use it as many times and different ways as you like in the PHP application code.

更新固定代码,因此它应与MySQLi而不是PDO一起使用.另外,将结果放入关联数组中.

Update Fixed code so it should work with MySQLi rather than PDO. Also, made the results into an associative array.

$results = array();
while($getEvent->fetch())
{
    $results[] = array('eventMember'=>$eventMember, 'eventStaff'=>$eventStaff, 'eventMemberID'=>$eventMemberID, 'eventMemberSuper'=>$eventMemberSuper);
}

// Now use $results however you need to.

这篇关于重置MySqli指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆