I am trying to fillup two select boxes using the result of a mysql select query. I run the query once and use the output variable to loop and set options of select tag accordingly. The problem is, first select box gets populated as expected but the second one does not. It remains empty whatsoever. Heres my code
<?php
$con = mysql_connect('localhost','root','') or die('Could Not Connect'.mysql_error());
mysql_select_db('irctc', $con)or die('Could Not Select'.mysql_error());;
$result = mysql_query("select * from stationcodes", $con)or die('Could not select'.mysql_error());
?>
<html>
<head>
</head>
<body>
<table>
<tr>
<td>Source</td>
<td>:-</td>
<td><select id='src'>
<option value=''>Select Source Station</option>
<?php while($row = mysql_fetch_array($result))
{ ?>
<option value='<?php echo $row['StationCode']; ?>'><?php echo $row['StationName']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td>Destination</td>
<td>:-</td>
<td><select id='dst'>
<option value=''>Select Destination Station</option>
<?php while($row = mysql_fetch_array($result))
{ ?>
<option value='<?php echo $row['StationCode']; ?>'><?php echo $row['StationName']; ?></option>
<?php } ?>
</select>
</td>
</tr>
</table>
</body>
</html>
You need to “rewind” data pointer back to the beginning before starting to iterate over the results again. Use
mysql_data_seek(0);to do that, before building your “Destination”:Also do not use
mysql_. Switch tomysqli_orPDO. and it would help you to separate code from view and use any type of template engine (likeSmarty)