How to select multiple rows from mysql with one query and use them in php

I currently have a database, as in the image below.

enter image description here

If there is a query that selects rows with number 1 equal to 1. When using

mysql_fetch_assoc() 

in php I only get the first one, is there any way to get the second one? How through a dimesional array, how

 array['number2'][2] 

or something similar

+7
source share
3 answers

Use repeat calls in mysql_fetch_assoc. It is documented in the PHP manual.

http://php.net/manual/function.mysql-fetch-assoc.php

 // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop // Note: If you put extract($row); inside the following loop, you'll // then create $userid, $fullname, and $userstatus while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } 

If you need, you can use this to create a multidimensional array for consumption in other parts of your script.

+13
source
 $Query="select SubCode,SubLongName from subjects where sem=1"; $Subject=mysqli_query($con,$Query); $i=-1; while($row = mysqli_fetch_array($Subject)) { $i++; $SubjectCode[$i]['SubCode']=$row['SubCode']; $SubjectCode[$i]['SubLongName']=$row['SubLongName']; } 

Here, the while loop will retrieve each row. All columns of the row will be stored in the variable $row (array), but when the next iteration happens, it will be lost. Therefore, we copy the contents of the $row array into a multidimensional array called $SubjectCode . The contents of each row will be stored in the first index of this array. This can be used later in our script. (I'm new to PHP, so if anyone came across this who knows a better way, write about it along with a comment with my name so that I can find out new.)

+2
source

This is another easy way.

 $sql_shakil ="SELECT app_id, doctor_id FROM patients WHERE doctor_id = 201 ORDER BY ABS(app_id) ASC"; if ($result = $con->query($sql_shakil)) { while ($row = $result->fetch_assoc()) { printf ("%s (%s)\n", $row["app_id"], $row["doctor_id"]); } 

Demo link

+1
source

All Articles