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.

  • 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. – iainn Dec 09 '17 at 12:48
  • @iainn That is my question, how can we get the output without a loop? –  Dec 09 '17 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. – iainn Dec 09 '17 at 12:56
  • @iainn I used this-->echo $results[0]['name'], echo $results[1]['name']. But it doesn't give the right output. –  Dec 09 '17 at 13:00
  • In the while loop, mysqli_fetch_array($data) is written as the condition, it isn't called multiple times. And I requested to get the output without a loop. –  Dec 09 '17 at 13:11
  • 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 – vicbyte Dec 09 '17 at 13:13
  • The statement mysqli_fetch_array($data) is outside the scope of the loop so it can't be called multiple times, I was replying to your first comment. –  Dec 09 '17 at 13:15
  • 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? – vicbyte Dec 09 '17 at 13:17
  • When you said it is called multiple times, I took it that you meant it is called each time to output the value. –  Dec 09 '17 at 13:25
  • @timothy-smith You can use mysqli_fetch_all to get all data as array without while loop. Please check my answer. – Amit Gupta Dec 09 '17 at 13:39

3 Answers3

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

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • 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']). –  Dec 09 '17 at 12:56
  • Can you plz write the code to get the desired results? –  Dec 09 '17 at 13:01
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
}
Amit Gupta
  • 2,771
  • 2
  • 17
  • 31
  • 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? –  Dec 09 '17 at 13:47
  • 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. – Amit Gupta Dec 09 '17 at 13:48
  • mean to say you can skip one loop (while loop) but need to add another one.. haha – Amit Gupta Dec 09 '17 at 13:49
  • 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 :) – Amit Gupta Dec 09 '17 at 13:50
  • 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. – Amit Gupta Dec 09 '17 at 13:52
  • Why is that not a correct approach? As long as you actually check if data exsists then this is as valid as using loop. Loop is much more convinient but life is not always that simple, is it? – vicbyte Dec 09 '17 at 13:53
  • Yes we can access data without it I mentioned earlier like $row[0]['name'] like you said but better approach is loop only. – Amit Gupta Dec 09 '17 at 13:54
  • @AmitGupta I tried your approach but I am not getting the output. –  Dec 09 '17 at 14:14
  • @TimothySmith Please let me know what issue you are facing. I will try my best to sort out your issue. – Amit Gupta Dec 09 '17 at 14:14
  • Have you seen this link :- https://stackoverflow.com/questions/10152920/php-multidimensional-array-get-values – Amit Gupta Dec 09 '17 at 14:15
  • You can get values from arrays of array or any multi dimensional array – Amit Gupta Dec 09 '17 at 14:18
  • @AmitGupta Thanks for all the help! –  Dec 09 '17 at 14:47
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
vicbyte
  • 3,690
  • 1
  • 11
  • 20
  • You mean to say, I must use, mysqli_fetch_array($data); echo $row[0]['name']; mysqli_fetch_array($data);echo $row[1]['name'];? –  Dec 09 '17 at 13:41
  • Used mysqli_fetch_array($data);echo $row['name']; mysqli_fetch_array($data);echo $row['name']; But I am getting an error. –  Dec 09 '17 at 13:51
  • You forgot to assing `mysqli_fetch_array` result to the $row; – vicbyte Dec 09 '17 at 13:52
  • $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. –  Dec 09 '17 at 13:59
  • @TimothySmith Please first check what's coming in array like echo "
    "; print_r($row); die;
    – Amit Gupta Dec 09 '17 at 14:02
  • @AmitGupta I am getting array of arrays in the output of rows. The problem is, if I want to get an out of a particular field, how to go about that manually without using any loops. –  Dec 09 '17 at 14:04
  • @TimothySmith In arrays of arrays or multi dimensional arrays, you can again get the output like $row['data']['name'] – Amit Gupta Dec 09 '17 at 14:08
  • @vicbyte Plz write precisely where you want me to add the statement. –  Dec 09 '17 at 14:10
  • You have an exact example to copy/paste. Assuming your database is in the correct format and code you submitted above was working it should work just by copying it without changes. I can't really help you more lol. – vicbyte Dec 09 '17 at 14:11
  • @TimothySmith Check this link I searched for you. It will give better idea to you:- https://stackoverflow.com/questions/10152920/php-multidimensional-array-get-values – Amit Gupta Dec 09 '17 at 14:13
  • @vicbyte did you try the code you gave me yourself? Cuz I did and it didn't work. –  Dec 09 '17 at 14:23