Reputation: 45
I am fetching my rows from database into webpage, but it is omitting the first row from database and showing results from 2nd row. what should I do to fix it? my code:
<?php
$connect = mysql_connect('localhost','root','');
mysql_select_db("try_test");
$query = "SELECT id, q_id, question,ans_options FROM test1";
$result=mysql_query($query);
if($record=mysql_fetch_array($result))
{
echo $record['q_id']. ". " ;
echo $record['question']."<br/>";
}
else{
echo "error";
}
while($record=mysql_fetch_array($result))
{
echo "<input type=radio>" ;
echo $record['ans_options']."<br/>";
}
?>
</body>
</html>
Upvotes: 0
Views: 1105
Reputation: 48
Do not use mysql_fetch_array more than once. Because, It fetches the row 1st and move the pointer to the next record. Than second time when you use that function it start fetching from that pointer`s position. So, You will not have your 1st record. Hope this help. I think @Abdulla Nilam has written the right code.
Upvotes: 1
Reputation: 799
Your query is fetching the 1st row, but its not followed by Radio button . So may be you are not able to read it. Comment the below part in your code then you will get all the rows
if($record=mysql_fetch_array($result)){
echo $record['q_id']. ". " ;
echo $record['question']."<br/>";
} else{ echo "error";}
If you still need this part of the code, Please let us know the purpose of it.
Upvotes: 0
Reputation: 38584
<?php
$connect = mysql_connect('localhost','root','');
mysql_select_db("try_test");
$query = "SELECT id, q_id, question,ans_options FROM test1";
$result=mysql_query($query);
if($record=mysql_fetch_array($result))
{
echo $record['q_id']. ". " ;
echo $record['question']."<br/>";
mysql_data_seek($result, 0);
while($record=mysql_fetch_array($result))
{
?>
<input type=radio><?php echo $record["ans_options"]; ?>
<br/>
}
<?php
}
else{
echo "error";
}
?>
bro can you try this and see.
Upvotes: 0
Reputation: 69439
With this line:
if($record=mysql_fetch_array($result))
you read the first line. So your while loop starts add the second line.
Upvotes: 0