I'd just add that you'd probably do better to separate your php and html as much as possible. Mixing them up can make pages hard to maintain, even if it looks more logical to do so.
<?php
$ops='';
$pdo = new PDO('mysql:host=localhost;dbname=contisec_portal', 'root', '');
$stmt = $pdo->query("SELECT dateid FROM date_header");
//$stmt = $pdo->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$ops.= "<option value='" . $row['dateid'] . "'>" . $row['dateid'] . "</option>";
}
?>
<!--later on-->
<select name="fileselect">
<?php echo $ops;?>
</select>
This means that you can place your code into a function, a class, an include file, whatever. No big deal though.
diafol
Keep Smiling
10,625 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
There are a couple of errors in your code ( to execute you need to prepare first and your execute should be $stmt->execute(), not $stmt = $pdo->execute())
I actually find using foreach with PDO tends to work better for some reason. I would also agree with diafol that you should separate your PHP and HTML but I would go even further than he has
<?php
$pdo = new PDO('mysql:host=localhost;dbname=contisec_portal', 'root', '');
$sql = "SELECT dateid FROM date_header";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($stmt->rowCount() > 0) { ?>
<select name="fileselect">
<?php foreach ($results as $row) { ?>
<option value="<?php echo $row['dateid']; ?>"><?php echo $row['dateid']; ?></option>
<?php } ?>
</select>
<?php } ?>
This is not tested
simplypixie
Practically a Master Poster
642 posts since Oct 2010
Reputation Points: 157
Solved Threads: 118
Skill Endorsements: 5
Yes - the foreach should be used with fetchAll(). +1
diafol
Keep Smiling
10,625 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57