2

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.

10
  • 2
    Each call to mysqli_fetch_array returns 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 use mysqli_fetch_all instead, although you'll obviously still have to loop over the result set to do anything with it. Commented Dec 9, 2017 at 12:48
  • @iainn That is my question, how can we get the output without a loop? Commented Dec 9, 2017 at 12:50
  • Well, you can call echo $results[0]['name'], echo $results[1]['name'], etc. But that's exactly what loops are designed to avoid. If you use fetch_all then you can put all the names in an array with array_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. Commented Dec 9, 2017 at 12:56
  • 1
    Well, you should learn how the loops work then. Condition is checked before (or after, depending on a loop) EACH run, meaning that mysql_fetch_array is run BEFORE EACH run Commented Dec 9, 2017 at 13:13
  • 1
    mysql_fetch_array is in the condition, and is called EVERY TIME BEFORE WHILE LOOP RUNS. How do you imagine checking the condition otherwise then? Commented Dec 9, 2017 at 13:17

3 Answers 3

1

You can use mysqli_fetch_all to get all data.

Check in php docs

-> mysqli_result::fetch_all -- mysqli_fetch_all — -> Fetches all result rows as an associative array, a numeric array, or both

Sign up to request clarification or add additional context in comments.

2 Comments

I read through the link but it states, " fetches all result rows and returns the result set as an associative array", whereas I am looking to get the output of a particular column(eg['name']).
Can you plz write the code to get the desired results?
0

First of all, this code:

while($row=mysqli_fetch_array($data))
{
    echo $row['name'];
}

is not equivalent to:

$row=mysqli_fetch_array($data);
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];
echo $row['name'];

But rather to something like (pseudocode):

$row = mysqli_fetch_array($data);
if (!$row) return; //break the loop
echo $row['name'];

$row = mysqli_fetch_array($data);
if (!$row) return; //break the loop
echo $row['name'];
...

And you could just use multiple mysqli_fetch_array() calls to achieve what you need, but there are other ways to achieve it. One of which is the usage of mysqli_fetch_all(). Both work in a similar way, but mysqli_fetch_all() returns an array of all results you received from the query, rather than one result at a time. Now, it returns an array of arrays, so to access it, rather than:

$row['name'];

You would need to use:

$row[ROW_NUMBER]['name']
//example
$row[0]['name']

Examples:

1) mysqli_fetch_array()

$row = mysqli_fetch_array($data);
echo $row['name']; //output name from the first row
$row = mysqli_fetch_array($data);
echo $row['name']; //output name from the second row

2) mysqli_fetch_all()

$row = mysqli_fetch_all($data);
echo $row[0]['name']; //output name from the first row
echo $row[1]['name']; //output name from the second row

11 Comments

You mean to say, I must use, mysqli_fetch_array($data); echo $row[0]['name']; mysqli_fetch_array($data);echo $row[1]['name'];?
Used mysqli_fetch_array($data);echo $row['name']; mysqli_fetch_array($data);echo $row['name']; But I am getting an error.
You forgot to assing mysqli_fetch_array result to the $row;
$row=mysqli_fetch_array($data); echo $row['name']; mysqli_fetch_array($data); echo $row['name'];--->Still don't get the output. Used mysqli_fetch_assoc(), but still no output.
@TimothySmith Please first check what's coming in array like echo "<pre>"; print_r($row); die;
|
0

You can use mysqli_fetch_all to get all data in array but you might need to use foreach for further action.

$con=mysqli_connect("localhost", "root", "", "student_details");
$data=mysqli_query($con,"select * from `registration`");
$row = mysqli_fetch_all($data, MYSQLI_ASSOC);

echo "<pre>"; print_r($row); die;
// Will show all data coming in $row as Array

For further processing, you might need to use foreach like below:

foreach ($row as $item) {
    print_r($item);  // see what your item contains
}

12 Comments

I got the output till the first code snippet my friend, but I intend to understand how do we fetch each record from $row, which is array of arrays?
ohh nice.. yeh actually even if you get all data as array with mysqli_fetch_all , you need to use foreach loop again to process it as its array.
mean to say you can skip one loop (while loop) but need to add another one.. haha
loop is required because you are working on multiple items and for processing or taking any action on them you have to use it my dear friend :)
And yes you can do it without foreach loop as well by getting total number of rows but then you have use like $item[0]['name'],$item[1]['name'] that is not a correct approach of doing.
|

Your Answer

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