PDO为foreach()提供的参数无效 [英] PDO Invalid argument supplied for foreach()

查看:132
本文介绍了PDO为foreach()提供的参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你们可以请帮忙,
即时尝试切换到新的PDO,但很难... ...

为什么这个代码工作:

  include('connect.php'); 

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

foreach($ conn as $ row)
{
print $ row ['Prenom']。 ''。 $ row ['Nom']。 '< br>'。
'类型:'。 $ row ['Type']。 '< br>'。
'电话:'。 $ row ['Tel']。 '< br>'。
'邮件:'。 $ row ['Mail']。 '< br>'。
'Bateau:'。 $ row ['Bateau']。 '< br>'。
'< br>< br>';

code $

$ b $ p

而不是这个:

  include('connect.php'); 

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

foreach($ conn as $ row)
{
print $ row ['Prenom']。 ''。 $ row ['Nom']。 '< br>'。
'类型:'。 $ row ['Type']。 '< br>'。
'电话:'。 $ row ['Tel']。 '< br>'。
'邮件:'。 $ row ['Mail']。 '< br>'。
'Bateau:'。 $ row ['Bateau']。 '< br>'。
'< br>< br>';

我在PHPMYADMIN中试过这些查询都可以,第二个应该显示一个结果,而是我没有得到什么,在我的错误日志中,我得到:
为foreach()提供的无效参数

解决方案

你的sql是错误的。

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

您可以设置异常模式,然后抛出异常。



或者您需要检查结果,如果返回false,请检查错误信息。


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()

解决方案

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.

这篇关于PDO为foreach()提供的参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆