<?php
$dbc=mysqli_connect('localhost','root','','sam_telephone');


$query="SELECT * FROM master_name";
$result=  mysqli_query($dbc,$query);

$display=mysqli_fetch_array($result);

echo $display['f_name']; /// its show me the result  which is fine 

// but when i apply while loop it does not show any thing.

while($y = mysqli_fetch_array($result)){
    echo $y['f_name'];
}
// $y['f_name'] should give me a result becuase it showing me the result on line
10 why its not working on this line i dont know 



?>

Recommended Answers

All 6 Replies

Are you sure that there is more than one result being returned?

Member Avatar for diafol

^^ By the second mysqli_fetch call the pointer is on the second record, so if only one record is returned, the second call will be empty. I think.

thanks Assembely Guys---- the things is that when i tried to get the result for echo $display['f_name']; (in line 10) it work fine it give the result, but it does not give me the result for $y['f_name']; in line 15

the things is that when i delete my line no. 10 which is $display['f_name']; than it give me result for $y['f_name'];

i think that wat difol is mentoning, but i did not understand fully what he is saying.

as i see it i echo the result once so it should show me the result, than i ran the while loop and want echo some thing so it should do that too,

diafol what do you meen by this

Yes @diafol is correct,you can only iterate the result once using mysqli_fetch_array(),however if for some reason you still want to call the query again,then before calling it another time set the pointer back to the beginning using mysqli_data_seek() method .

So just reset the result pointer to the beginning

<?php
   $dbc=mysqli_connect('localhost','root','','sam_telephone');
$query="SELECT * FROM master_name";
$result=  mysqli_query($dbc,$query);
$display=mysqli_fetch_array($result);
echo $display['f_name']; /// its show me the result  which is fine 
// but when i apply while loop it does not show any thing.
mysqli_data_seek($result, 0);
while($y = mysqli_fetch_array($result)){
    echo "here". $y['f_name'];
}
// $y['f_name'] should give me a result becuase it showing me the result on line
//10 why its not working on this line i dont know 
?>
Member Avatar for diafol

The question is, why are you fetching twice? D you really need to do that?

thanks IIM yah its work fine, and thanks for explanation too,
diafol and no i dont have to fetch it twice, i was just checking that weather the query is working or not in the 10th line and than i got confused why i did not work second time, but its good i learn some thing by my mistakes

Thanks for help guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.