When I used while loop to print the values of a particular column using mysqli_fetch_array(), I got the correct output from the database.
while($row=mysqli_fetch_array($data))
{
echo $row['name'];
}
But when I used the below mentioned code without the loop, the output was different.
$row=mysqli_fetch_array($data);
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
When I used the below mentioned code, I could see that I get all the rows in the form of an associative array using mysqli_fetch_array().
$con=mysqli_connect("localhost", "root", "", "student_details");
$data=mysqli_query($con,"select * from `registration`");
while ($row = mysqli_fetch_array($data))
{
$rec[]=$row;
}
echo "<pre>";
print_r($rec);
echo "</pre>";
?>
My question is how to get all the values of a particular column (using a while loop), without usage of any loop.
mysqli_fetch_arrayreturns the next row in the result-set. If you only call it once, you'll only ever retrieve the first row from the database. If you don't want to use a loop to fetch the results, you can usemysqli_fetch_allinstead, although you'll obviously still have to loop over the result set to do anything with it.echo $results[0]['name'],echo $results[1]['name'], etc. But that's exactly what loops are designed to avoid. If you usefetch_allthen you can put all the names in an array witharray_column, but unless you know how many rows are in the set then you're always going to have to use a loop to process them at some point. I'm not really sure what problem you're trying to solve and why.mysql_fetch_arrayis run BEFORE EACH runmysql_fetch_arrayis in the condition, and is called EVERY TIME BEFORE WHILE LOOP RUNS. How do you imagine checking the condition otherwise then?