-3

I've been trying to populate a dropdown list with results from a query using php from a mysql table but the list is empty. Could someone please take a look at my code and tell me what it is that I'm doing wrong.

My query is Select cat from kernel

I used the code : $results = $query->fetchAll();

Heres my code:

<?php

$dbhandle = new PDO('host','username','password');


$sql = "SELECT cat FROM kernel";
$results = $query->fetchAll();

echo "<select name='cat'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['cat'] . "'>" . $row['cat'] . "</option>";
}
echo "</select>";

?>
7
  • are there results in your query? If so you're going to need to post a lot more code before we can help. Commented Mar 31, 2014 at 2:35
  • What does $results return ? Commented Mar 31, 2014 at 2:35
  • This is a duplicate. Check here: stackoverflow.com/questions/5327235/… Commented Mar 31, 2014 at 2:36
  • Is your query already working? Nobody will solve your problem entirely.. you must provide more information! I Suggest you to google a litle bit more! :P Commented Mar 31, 2014 at 2:37
  • you're off to a good start, now do something with the results. :) Commented Mar 31, 2014 at 2:37

1 Answer 1

0
<?php

$sth = $dbhandle->prepare("SELECT cat FROM kernel");
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC);

echo "<select name='cat'>";
foreach($results as $row) {
    echo "<option value='" . $row['cat'] . "'>" . $row['cat'] . "</option>";
}    
echo "</select>";

?>

https://www.php.net/manual/en/pdostatement.fetchall.php

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked! there is an extra bracket close at the line: "$results = $sth->fetchAll(PDO::FETCH_ASSOC));" which should be "$results = $sth->fetchAll(PDO::FETCH_ASSOC);"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.