Trying to populate a dropdown with PDO, not working

I need some help with my dropdown, im trying to populate it but its not working.

[php]$sql = “SELECT publisher_name FROM publishers WHERE publisher_id=?”;
$stmt = $pdo->prepare($sql);

    $result = $stmt->fetch(PDO::FETCH_ASSOC);[/php]

My dropdown


<option>----- Select -----</option>
<?php foreach($result as $option) : ?>
<option value="<?= $option->publisher_id; ?>"><?= $option->publisher_name; ?></option>   
<?php endforeach ?>

That dident work.

This is what i had before you gave me that.

This is for my dropdown to search which works.

<?php try { require_once("./config.php"); $_POST = array_map('trim', $_POST); //Testing $sql = "SELECT publisher_name FROM publishers WHERE publisher_id=?"; $stmt = $pdo->prepare($sql); $stmt->execute(array($_POST['publisher_id'])); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); if (count($result)) { foreach ($result as $row) { echo '
             echo '
'; } } else { echo "No rows returned."; } } catch (PDOException $e) { include_once('./config/pdo_catch_error.php'); } ?>[/php]

But i want to populate it instead of having to write all the values and names out.


<option>----- Select -----</option>
<?php foreach($result as $key => $value){?>
<option value="<?= $key;?>"><?= $value;?></option>  
<?php }?>
<option value="1">DC</option>
<option value="3">Dark Horse</option>
<input type="submit" value="Search">

No i havent changed it, here it is:

[php]CREATE TABLE IF NOT EXISTS publishers (
publisher_id int(11) NOT NULL AUTO_INCREMENT,
publisher_name varchar(255) NOT NULL,
PRIMARY KEY (publisher_id,publisher_name),
KEY publisher_id (publisher_id)

INSERT INTO `publishers` (`publisher_id`, `publisher_name`) VALUES (1, 'DC'), (2, 'Marvel'), (3, 'Dark Horse'), (4, 'Dark Horse Manga '), (5, 'Disney Comics'), (6, 'Dragon Lady Press'), (7, '12 bis'), (8, '12-Gauge Comics'), (9, '215 Ink'), (10, 'Abacus Comics'), (11, 'About Comics'), (12, 'Academy Comics'), (13, 'AC Comics'), (14, 'ACE Comics'), (15, 'Ace Comics'), (16, 'Action Lab Entertainment'), (17, 'Action Lab Signature Series '), (18, 'AdHouse Books'), (19, 'Adventure Publications '), (20, 'After Hours Press'), (21, 'Aircel Comics'), (22, 'AiT/Planet Lar'), (23, 'Alias Comics'), (24, 'All Star DC Comics'), (25, 'Alterna Comics'), (26, 'Alternative Comics'), (27, 'Angry Viking Press'), (28, 'Antarctic Press');

Its still not working I dont understand why, its like its there but its not showing up as text.

Are you using ONLY the code I posted and running that all by itself? It works perfectly. There is nothing more I can do. I have attached the file to make sure you have the right code. Run it all by itself.

publishers.txt (904 Bytes)

Yes i am only using the code you gave me and changing the db and putting my password in but im still not getting it to show up as text.

$hostdb = ‘localhost’;
$dbname = ‘search’;
$username = ‘root’;
$password = ‘’;
$table = ‘publishers’;

$pdo = new PDO(“mysql:host=localhost;dbname=$dbname”, $username, $password);
$sql = “SELECT * FROM $table”;
$stmt = $pdo->prepare($sql);

$result = $stmt->fetchAll();


----- Select ----- <?php foreach ($result as $row){?> <?= $row['publisher_name'];?> <?php }?> <?php } catch (PDOException $e) { echo 'ERROR: ' . $e->getMessage(); //echo $e; //include_once('../config/pdo_catch_error.php'); } ?>[/php]

I don’t think this is the problem but it wouldn’t hurt to try.? Sometimes local servers act funny (or web servers as a matter of fact).

instead of

[php]<?= $row['publisher_name'];?>[/php]

[php]<?php echo $row['publisher_name']; ?>[/php]

The only thing I can do at this point is look at it where it sits on your server if you can provide a temporary login. Is there a URL I can see your form?

  • UPDATE: Do what Strider64 said. If you are on older PHP that would be the problem. I started using short tags instead of echo. They are enabled by default on PHP ver 5.4 and on. Upgrade your php.

  • Thanks Strider64, I am pretty sure that is the exact problem. It’s actually not about local server or not, just PHP version.

A new version of PDO Bumpstart is now available for download. I added a few additional features to learn from. It now has a database dropdown select, form to email and PHP Info.

You can download it here:!

Thanks Strider64 it works perfectly.

Thanks kevin Rubio for the PDO bumpstart :slight_smile:

Your welcome. If you can, you should update your PHP. The new PDO bumpstart download will show you what version you have if you dont already know.

Sponsor our Newsletter | Privacy Policy | Terms of Service