1

I'm trying to make a drop down list with the data of one of my columns...i'm wandering if the code below is doing that... (it doesn't work by the way) Thanks very much!

<select id="teamlist" name="teamlist">
<?php
        $pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
    #Set Error Mode to ERRMODE_EXCEPTION.
    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  



$stmt = $pdo->prepare('Select teamname from members');

   while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
     echo "<option>$row</option>";
   }
 ?>
</select> 
3
  • well it's giving me an empty list.. Commented Jun 1, 2012 at 8:51
  • 1
    I'm not familiar with PDO but is $row really the value you want to use? or do you need a sub item of $row (as in $row[0]) ? Commented Jun 1, 2012 at 8:51
  • tried that just then.. still nothing.. Commented Jun 1, 2012 at 8:52

3 Answers 3

3

You must execute your statement first before you can fetch results.

$stmt->execute();

or you can use query

$pdo->query('select ... ');

You can read more here and here

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

Comments

0

Referring this link

You must execute the prepare statement as follows.

$sql = 'SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));
$red = $sth->fetchAll();
$sth->execute(array(':calories' => 175, ':colour' => 'yellow'));
$yellow = $sth->fetchAll();

Comments

0

your pdoStatement::fetch returns an associative array, you need to select the correct value in your array, not just give the array itself to print, which as you noticed prints as nothing.

echo "<option>{$row['teamname']}</option>";

is what you need

3 Comments

Rawkode is right that you still need to issue execute() first as well, don't forget to do that as well
its giving me 6 options.. the same amount as the amount of entries in the column.. but no words..
Did you use fetchall because that call will return an array of all the rows retrieved by the query at once instead of one row at a time like fetch. You'd use $row[<rownumbere here>]['name'] in such a case.

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.