Return the exact ID using fetch_assoc

Posted by Selom on Stack Overflow See other posts from Stack Overflow or by Selom
Published on 2010-06-14T09:21:39Z Indexed on 2010/06/14 9:32 UTC
Read the original article Hit count: 168

Filed under:
|

Hi, I have a small problem in my code and I need your help. Well Im using fetch_assoc to get data from database and I need to get the ID number of each of returned values. the issue is that my code only return the ID number of the last data. Here's my code:

<form method="post" action="action.php">

  <select name="album" style="border:1px solid #CCC; font-size:11px; padding:1px">

       <?php

          $sql = "SELECT * FROM table";
          $stmt = $dbh -> prepare($sql);
          $stmt -> execute();

          while($row = $stmt -> fetch(PDO::FETCH_ASSOC))
          {

            $album_ID = $row['album_ID'];
            $value = $row['album_name'];         
            print "<option value ='". $value ."'>". $value. "</option>";


          }

       ?>

  </select>
  <input type="hidden" name="album_ID" value="<?php print $album_ID?>"/>
</form>

I would like the hidden input type holds the selected album id, but it always holds the album id of the last data. Please help.

© Stack Overflow or respective owner

Related posts about php

Related posts about pdo