简体   繁体   English

为foreach()提供了无效的参数

[英]Invalid argument supplied for foreach()

i'm coding a project and have some troubles when the system inform error: Invalid argument supplied for foreach() in: foreach($dbh->query($q1) as $row) 我正在编码一个项目,并且在系统通知错误时遇到一些麻烦:为foreach()提供了无效的参数:foreach($ dbh-> query($ q1)as $ row)

and can't get data from the database. 并且无法从数据库中获取数据。 How can i fix it, im a newbie, so if i don't understand, please teach me! 我是新手,我该如何解决,如果我不明白,请教我! thanks! 谢谢! thank for your helps but i still can't fix it 感谢您的帮助,但我仍然无法解决

<?php include("top.html"); ?>

<body>

    <div id="main">
        <h1>Results for <?php echo $_GET['firstname'] . " " . $_GET['lastname'] ?></h1> <br/><br/>
        <div id="text">All Films</div><br/>
        <table border="1">
            <tr>
                <td class="index">#</td>
                <td class="title">Title</td>
                <td class="year">Year</td>
            </tr>
            <?php

    $dbh = new PDO('mysql:host=localhost;dbname=imdb_small', 'root', '');


    $q1 = "SELECT id 
            FROM actors 
            WHERE first_name = '".$_GET['firstname']."'  AND last_name = '".$_GET['lastname']."' 
            AND film_count >= all(SELECT film_count 
                                    FROM actors 
                                    WHERE (first_name LIKE'".$_GET['firstname']." %' OR first_name = '".$_GET['firstname']."') 
                                    AND last_name = '".$_GET['lastname']."')";
    $id = null;

    foreach($dbh->query($q1) as $row){
        $id = $row['id']    ;
    }
    if($id == null){
        echo "Actor ".$_GET['firstname']." ".$_GET['lastname']."not found.";  
    }
    `
                $sql2 = "SELECT m.name, m.year 
                 FROM movies m 
                 JOIN roles r ON r.movie_id = m.id 
                 JOIN actors a ON r.actor_id = a.id 
                 WHERE (r.actor_id='".$id."') 
                 ORDER BY m.year DESC, m.name ASC";

            $i = 0;
            foreach($dbh->query($sql2) as $row){
                echo "<tr><td class=\"index\">";
                echo $i+1;
                echo "</td><td class=\"title\">";
                echo $row['name'];
                echo "</td><td class=\"year\">";
                echo $row['year'];
                echo "</td></tr>";
                $i++;
            }
            $dbh = null;
            ?>

        </table>

    </div>
</div>
<?php include("bottom.html"); ?>
</body>
</html>

You could do this 你可以这样做

$st = $dbh->query($q1);
if ( $st ) {
    while ( $row = $st->fetch() ) {}
}

Try to make your code more readable 尝试使您的代码更具可读性

Check that your query succeeded before iterating on the result: 在迭代结果之前,请检查查询是否成功:

if (false !== ($result = $dbh->query($d1))) {
    foreach($result as $row){
        $id = $row['id']    ;
    }
}

By the way, I don't understand what you are trying to do with this pointless loop. 顺便说一句,我不明白您在尝试这种无意义的循环。

$result = $dbh->query($q1);

foreach($result as $row){$id = $row['id'];}

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

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