0
votes

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
Set PDO to throw exceptions on errors. Your problem will become obvious. - DCoder

1 Answers

3
votes

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.