My dropdown list shows empty. I am not sure if my query returns any records. How can I find out if my query returned any result? Here is my code. Please let me know. I am stuck with this issue.
<?php $link = mysql_connect("localhost", "root", "root");
if (!$link) { die('Could not connect: ' . mysql_error()); }
echo 'Connected successfully';
mysql_select_db("mydb");
$sql = "SELECT state_id, state_code FROM states";
$result = mysql_query($sql) or die(mysql_error());
?>
<p>
<select name="vers">
<?
while($row = mysql_fetch_array($result)){
echo "<option value=\"".$row['state_id']."\">".$row["state_code"]."</option>";
}
?>
</select>
<?
mysql_close($link);
?>
Test the success or failure of your query with
if (!$result)and usemysql_num_rows()to verify the number of rows returned. Rather thandie(), you can display a better error to your user and possibly render different HTML, rather than just exit the script abruptly.UPDATE
Add an empty default option. Make sure its
valueattribute is not something you would retrieve from your database.