0

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?

3
  • Also, note, you should switch to the mysqli_ or PDO extension as the mysql_ extension is going to be deprecated as of PHP 5.5.0. It is an easy change and good to do it as you start out rather than later on. :-) Commented Jun 13, 2014 at 13:42
  • I don't really follow what 'second data' is. You mean you want to skip the first record altogether? Just filter it out in your SQL. use the LIMIT clause: LIMIT 999, 1. Or use WHERE id > 1. Commented Jun 13, 2014 at 13:46
  • how to switch to mysqli? @Richard Testani, i don't want to skip the first, what i want is to show all data.. Commented Jun 13, 2014 at 14:05

1 Answer 1

2

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)){ 
Sign up to request clarification or add additional context in comments.

4 Comments

i did this many times but still the first data is missing.. how to set off?
I did. I even showed you how to fix it. What part of my answer do you not understand that I might need to clarify for you?
do i have to erase this block of code? $data = mysql_fetch_array($result);
Yes. That is exactly what you need to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.