Jay-Ar Aclan
Jay-Ar Aclan

Reputation: 61

How to access data from database and make an option value?

I have this code

$result = mysql_query("SELECT name from room");
$data = mysql_fetch_array($result); 
$firstValue="";
while($data = mysql_fetch_array($result)){ 
    if($firstValue==""){ 
        $firstValue=$data['name'];
    }
    if(isset($_POST["occupant"])and trim($_POST["choice"])==$data['name']){
        echo '<option selected="selected" value="'.$data['name'].'" >'.$data['name'];
        echo '</option>';
    }else
        {
         echo '<option value="'.$data['name'].'" >'.$data['name'];
         echo '</option>';
         }
    }

What i want here is to show all data from my database but when i open it in browser shows only the second data down to the last data. And i wonder why the first data is missing. Can anybody knows my mistake here?

Upvotes: 0

Views: 46

Answers (1)

John Conde
John Conde

Reputation: 219824

Get rid of the first:

$data = mysql_fetch_array($result); 

It is popping the first record off of your result set.

$result = mysql_query("SELECT name from room");
$firstValue="";
while($data = mysql_fetch_array($result)){ 

Upvotes: 2

Related Questions