简体   繁体   English

遍历MySQL结果PHP

[英]Loop through MySQL results PHP

So, I'd like to make a loop that loops through the results from the following code: 因此,我想做一个循环,遍历以下代码的结果:

$check_events = "SELECT * FROM events_meedoen WHERE username=:username";
    $stmt_check = $db->prepare($check_events);
    $stmt_check->bindParam(':username', $username);
    $stmt_check->execute();

    $events_row = $stmt_check->fetch(PDO::FETCH_BOTH);
    $event_id = $events_row['event_id'];

    $get_event = "SELECT * FROM agenda WHERE event_id=:event_id";
    $stmt = $db->prepare($get_event);
    $stmt->bindParam(':event_id', $event_id);
    $stmt->execute();

So, for example, I'd like to get all results with the $event_id gotten by checking the agenda for all events that the user is signed up for. 因此,例如,我想通过检查用户注册的所有事件的议程来获得$event_id所有结果。

This should result in 2 for my account, which it does not because it only checks once. 这应为我的帐户产生2,但不是,因为它只检查一次。 Any help? 有什么帮助吗? :) :)

you need to put your call to fetch in a while loop. 你需要把你的电话,以fetch在while循环。 As you can see from the docs , PDO::fetch calls the next record from the result set. 文档中可以看到, PDO::fetch调用结果集中的一条记录。

so, something like this will loop through all of the results: 因此,类似这样的事情将遍历所有结果:

while($events_row = $stmt_check->fetch(PDO::FETCH_BOTH)) {
    $event_id = $events_row['event_id'];
    // do something with the $events_row array here
}

because PDO::fetch returns false when it hits the end of the result set, the while loop knows when to stop. 因为PDO::fetch到达结果集的末尾时将返回false ,因此while循环知道何时停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM