简体   繁体   English

foreach() 提供的参数无效

[英]foreach() Invalid argument supplied

I have this code that works for my friend but when I run it, it gave this Warning: Invalid argument supplied for foreach() in C:\wamp64\www\DVD_show.php on line 10我有这个代码适用于我的朋友,但是当我运行它时,它给出了这个警告:在第 10 行为 C:\wamp64\www\DVD_show.php 中的 foreach() 提供的参数无效

What is the problem?问题是什么?

    '''
    <?php
    try {
        /*** connect to SQLite database ***/
        $dbh = new PDO("sqlite:dvd.db");
    //echo("ok");
    if(isset($_GET['name'])){
    $name=$_GET['name'];
    $sql = "SELECT * FROM DVD where name='".$name."'";
    }else $sql = "SELECT * FROM DVD";
        foreach ($dbh->query($sql) as $row)
            {
            print 'dvds[index++]="#'.$row['name'] ."#".$row['director']. "#".                 $row['price']."#".$row['stock'].'#";<br>';
            //dvds[index++]="#Life is Beautiful#dvd1#10.5#10#history#Roberto Benigni#";
            }
    
        /*** close the database connection ***/
        $dbh = null;
        }
    catch(PDOException $e)
        {
        echo $e->getMessage();
        }
    ?>
    <form action="http://127.0.0.1/DVD_show.php" method="get">
     <p>Please input DVD name: <input type="text" name="name" /></p>
    
     <p><input type="submit" /></p>
    </form>
    '''

$dbh->query($sql) will return you Statement . $dbh->query($sql)将返回Statement You should use fetch or fetchAll to get the results.您应该使用fetchfetchAll来获取结果。 While fetch will return only one - so not iterable, you should use fetchAll , which always returns array.虽然fetch只会返回一个 - 因此不可迭代,但您应该使用fetchAll ,它始终返回数组。

The dbh-> query is wrong at this place as it only returns a statment but not something you can use with foreach dbh-> 查询在这个地方是错误的,因为它只返回一个语句,而不是你可以与 foreach 一起使用的东西

You first need to use the fetch() or fetchAll() methods您首先需要使用 fetch() 或 fetchAll() 方法

Please see changed code.请查看更改后的代码。

NOTE: If you have a HIGH number of results it is not recommended to use fetchAll as it loads the wohle dataset.注意:如果您有大量结果,则不建议使用 fetchAll,因为它会加载 wohle 数据集。 then better use fetch() and while like shown in the example ar the link below.然后更好地使用 fetch() 并且像下面的示例中所示的链接。

  <?php
    try {
        /*** connect to SQLite database ***/
        $dbh = new PDO("sqlite:dvd.db");
    //echo("ok");
    if(isset($_GET['name'])){
    $name=$_GET['name'];
    $sql = "SELECT * FROM DVD where name='".$name."'";
    }else $sql = "SELECT * FROM DVD";
    
//change this 
    
$data =  $dbh->query($sql) ->fetchAll();
        foreach ($data as $row)

//changes end 
            {
            print 'dvds[index++]="#'.$row['name'] ."#".$row['director']. "#".                 $row['price']."#".$row['stock'].'#";<br>';
            //dvds[index++]="#Life is Beautiful#dvd1#10.5#10#history#Roberto Benigni#";
            }

        /*** close the database connection ***/
        $dbh = null;
        }
    catch(PDOException $e)
        {
        echo $e->getMessage();
        }
    ?>
    <form action="http://127.0.0.1/DVD_show.php" method="get">
     <p>Please input DVD name: <input type="text" name="name" /></p>

     <p><input type="submit" /></p>
    </form>
    '''

please see:请参见:

https://phpdelusions.net/pdo_examples/select https://phpdelusions.net/pdo_examples/select

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

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