PHP / PDO - Use PDO statement multiple times when using multiple foreach loops

感情迁移 提交于 2020-02-25 13:51:15

问题


I have the following code:

if(!empty($postCountryAdd)) {
$sqlQueryLocalityAdd = $dbh->prepare("SELECT DISTINCT locality_add FROM table WHERE country_add = :country_add ORDER BY locality_add ASC");
$sqlQueryLocalityAdd->execute(array(':country_add' => $postCountryAdd));

echo '<option value="">Select locality</option>';
    foreach($sqlQueryLocalityAdd as $localityAddRow) {
        //while ($localityAddRow = $sqlQueryLocalityAdd->fetch()){
        echo '<option value="';
        echo $localityAddRow["locality_add"];
        echo '">';
        echo $localityAddRow["locality_add"];
        echo '</option>';
    }
}

If I use foreach($sqlQueryLocalityAdd as $localityAddRow) the code stops responding. Why can't I use foreach more than once? How can I fix it please?


回答1:


$sqlQueryLocalityAdd is an Object which in this case - which I am showing and OP has used - cannot be iterated through. (bold so @deceze can understand).

You can use the fetchAll() inside a foreach loop to achieve this however.

Your code should look something like this:

[...]
    if($sqlQueryLocalityAdd->execute(array(':country_add' => $postCountryAdd)):
        foreach($sqlQueryLocalityAdd->fetchAll() as $row):
            echo $row['column'];
            [...]
        endforeach;
    endif;
[...]

Difference between an array and an Object




回答2:


The problem is that the result set can only be iterated once; it is then exhausted, MySQL discards it, and you cannot iterate it again.

I have never actually tried this, but to create a result set which can be iterated several times, you need a scrollable cursor, which you should be able to create thusly:

$sqlQueryLocalityAdd = $dbh->prepare(..., [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);

You should then theoretically be able to iterate the result multiple times.

If that doesn't work, fetch the data into an array using $sqlQueryLocalityAdd->fetchAll() and iterate it as often as you want. In fact, that appears to be the only option with MySQL: https://stackoverflow.com/a/19076778/476




回答3:


$sqlQueryLocalityAdd isn't a result set, it's a PDOStatement object and you can only iterate over it directly once (as noted clearly by deceze).

The execute() statement returns a boolean true or false on success of the query.

If successfully run, you need to fetch results from it post-query and iterate the array returned:

   $success = $sqlQueryLocalityAdd->execute(array(':country_add' => $postCountryAdd));
   if($success) {

       $results = $sqlQueryLocalityAdd->fetchAll();

       foreach($results as $localityAddRow) {
           echo '<option value="';
            ....

The resulting array $results is just a vanilla array, so you can iterate over it as many times as you want.

Note: If the execute() returns false, something is wrong with the query--a successful running query returning a empty result set results will still result in a true.



来源:https://stackoverflow.com/questions/36428412/php-pdo-use-pdo-statement-multiple-times-when-using-multiple-foreach-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!