PDO Invalid argument supplied for foreach()

萝らか妹 提交于 2019-12-25 10:19:14

问题


If you guys could please help, im trying to switch to the new PDO but having a hard time...

Why does this code work:

include ('connect.php');

$sql = "SELECT * FROM GP_2012";
$conn = $DBH->query($sql);

foreach ($conn as $row)
    {
    print $row['Prenom'] . ' ' . $row['Nom'] . '<br>' . 
    'Type: ' . $row['Type'] . '<br>' . 
    'Telephone: ' . $row['Tel'] . '<br>' .
    'Mail: ' . $row['Mail'] . '<br>' .
    'Bateau: ' . $row['Bateau'] . '<br>' .
    '<br><br>';
    }

.

And not this one:

include ('connect.php');

$sql = "SELECT * FROM GP_2012 WHERE Nom LIKE Pageot";
$conn = $DBH->query($sql);

foreach ($conn as $row)
    {
    print $row['Prenom'] . ' ' . $row['Nom'] . '<br>' . 
    'Type: ' . $row['Type'] . '<br>' . 
    'Telephone: ' . $row['Tel'] . '<br>' .
    'Mail: ' . $row['Mail'] . '<br>' .
    'Bateau: ' . $row['Bateau'] . '<br>' .
    '<br><br>';
    }

I tried in PHPMYADMIN and those queries both work, the second one should show one result but instead i get nothing and in my error log i get: Invalid argument supplied for foreach()


回答1:


Your sql is wrong.

$sql = "SELECT * FROM GP_2012 WHERE Nom LIKE '%Pageot%'";

You could set the exception mode, then exception will be thrown on error.

Or you need to check the result, if return false, check the error info.



来源:https://stackoverflow.com/questions/12988109/pdo-invalid-argument-supplied-for-foreach

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