MySQL Forums
Forum List  »  PHP

Re: Problem with populating Dropdown Menu with PDO
Posted by: Frank Olschewski
Date: December 10, 2015 04:07AM

Included it and it just shows nothing, just breaking the form at the point where it shows the dropdown menu, consisting of no options.

I included the catch PDOException for the connection code line and it works now, but I have no idea why - the catch PDOException is just for error reporting, isn't it?

So now I have:

<select name="subject">
<?php

try {
    $pdo = new PDO($dsn, $user, $pw);
    }  catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

    try
    {
        /*** query the database ***/
        $result = $pdo->query("SELECT subject_id, subject FROM subjects");

        /*** loop over the results ***/
        foreach($result as $row)
        {
            /*** create the options ***/
            echo '<option value="'.$row['subject_id'].'"';

            echo '>'. $row['subject'] . '</option>'."\n";
        }
    }
    catch(PDOException $e)
    {
        echo 'No Results';
    }
?>
</select>

Do I always have to include the catch PDOException to make the connection work? I am used to just do the connection code line as it was the case with mysql (mysql_connect etc.).

Greetings,
Frank

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.