Selecting MySql table data into an array
93,120
Solution 1
$sql = mysql_query("select id, name, code from users");
$userinfo = array();
while ($row_user = mysql_fetch_assoc($sql))
$userinfo[] = $row_user;
This will give you $userinfo
as an array with the following structure:
[
[id => 1, name => 'gorge', code => '2123'],
[id => 2, name => 'flix', code => 'ksd02'],
[id => 3, name => 'jasmen', code => 'skaod2']
]
If you want to output the data:
foreach ($userinfo as $user) {
echo "Id: {$user[id]}<br />"
. "Name: {$user[name]}<br />"
. "Code: {$user[code]}<br /><br />";
}
Solution 2
while($row_user = mysql_fetch_assoc($sql)){
$userinfo[] = $row_user;
}
foreach($userinfo as $usrinfo){
echo "Id: ".$usrinfo['id']."<br />";
echo "Name: ".$usrinfo['name']."<br />";
echo "Code: ".$usrinfo['code']."<br />";
}
Solution 3
I found this code and I save my day:
<?php
$sql=mysql_query("select * from table1");
/*every time it fetches the row, adds it to array...*/
while($r[]=mysql_fetch_array($sql));
echo "<pre>";
// Prints $r as array
print_r ($r);
echo "</pre>";
?>
Author by
Kareem Nour Emam
Updated on July 24, 2022Comments
-
Kareem Nour Emam almost 2 years
I try to catch data from mysql to put them all in array. Suppose:
users table ----------------------- id| name | code ---------------------- 1| gorge | 2132 2| flix | ksd02 3| jasmen | skaod2 $sql = mysql_query("select id, name, code from users"); $userinfo = array() while($row_user = mysql_fetch_array($sql)){ $userinfo = $row_user[name] } ------------------------- foreach($userinfo as $usinfo){ echo $usinfo."<br/>"; }
Here is the problem i can only insert user name but cant insert also code & id in userinfo array please help me to insert all data in same array.
[P.S] No object oriented please.
-
Christa over 13 yearsI'm sure you typed this really fast, but you are using mysql_query twice. That isn't going to work.
-
ThiefMaster over 13 yearsNo need to create a new array. Use mysql_fetch_assoc or pass MYSQL_ASSOC to mysql_fetch_array to get just the array you ar ebuilding. And strings (your array indices) belong in single quotes. Your code would throw 3 E_NOTICE notices per loop.
-
Yossi Gil over 13 years@user553786, this answers your question as I understand it. Please explain further what you are trying to do.
-
Kareem Nour Emam over 13 yearsi ask about how to insert id, name, code in array when while loop is work focus in ex
-
Kareem Nour Emam over 13 yearswhy u update ur first answer i think the first way is better than second way but both solution pleas
-
Christa over 13 yearsuser.. This line is going to do what you asked: $userinfo[] = $row_user; did you try it? The $row_user will contain the id, name, and code in it. Instead of being the variable $row_user it will be reassigned to the next array (as indicated by $userinfo[]). That's why both answers have the same solution.
-
Kareem Nour Emam over 13 yearsoky i try like u say $userinfo[] = $row_user; did you try it? yes i try and give me that ^^ Fatal error: Cannot use [] for reading
-
Yossi Gil over 13 years@user553786, updated answer to reflect how you would use the array for output.
-
Yossi Gil over 13 yearsCongratulations, you flew around the moon to get to the corner store.