Re: reuse my data source

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 5/31/2014 1:16 PM, ICCSI wrote:
I have following code to get data to show data and fill my select list.
The following code works either to fill list or show data in the table, but
not both.

It looks like the data show on the table then it reaches the end of data,
so the select list does not have anything to fill. I might be wrong.

If I am right, how can I move the data to BOF, or if I am wrong then is it
possible to have it reuse the data source second time on the form.

Your help and information is great appreciated,

Regards,


Iccsi,

<?php


$hostname = 'localhost';
$username = 'root';
$password = 'password';

try {
     $dbh = new PDO("mysql:host=$hostname;dbname="MyDB", $username,
$password);
       echo 'Connected to database';
      }
catch(PDOException $e)
     {
     echo $e->getMessage();
      }

   $stmt = $dbh->query('SELECT MyField from Mytable');
   while($data = $stmt->fetch(PDO::FETCH_ASSOC)){

                echo "<tr><td>".$data{'MyField'}."</td></tr>";

       }
   ?>

<select name='mylist' id='mylist' size='1'>

<?php

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

{

     echo "<option>".$row['MyField']."</option>";

}

?>

</select>

Well - you have to reset the query results in order to browse thru them a second time. Try doing a fetchall to create an array of your results and then use the foreach to loop thru them for your two needs.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux