Hi
I am learning PDO. I already got my PDO-DB ready.
The query works because it does fetch data when I echo out the data but I don't know how to echo out the data from the database to the dropdown list.
I try to echo out this way (it didn't work):
<select name="PostCats">
<?php
function getCats() {
global $conn;
$stmt = $conn->query('SELECT * FROM category');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value=".$row['id'].">".$row['title']."</option>";
}}?>
</select>
I also try to echo out this way too (it didn't work either):
<select name="PostCats">
<?php
function getCats() {
global $conn;
$stmt = $conn->query('SELECT * FROM category');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?></option>
<?php }} ?>
</select>
I want to learn the correct way of echoing out the data from the database with PDO or Mysqli to the dropdown list.
I have a habit of combining html and php and now I realized it's not good to combine both together.
I tend to do something like this:
<?php
$query = "SELECT * FROM category";
$result = mysql_query ($query);
echo "<select name=post value=''>POST NAME</option>";
while($mi = mysql_fetch_array($result)){
echo "<option value=$mi[id]>$mi[title]</option>";
}echo "</select>";
?>
Then I realized PDO & Mysqli doesn't work.
Any Suggestions and explanations will help. I appreciate it. Thanks!