I have record rows in MySQL as
id=0 name=tom grade=A
id=1 name=jeff grade=B
id=2 name=lisa grade=B
....... etc
Now I want to output that do a 2 dimensional array
such as
content{
{id=0
name=tom
grade=A
}
{id=1
name=jeff
grade=B}
{id=2
name=lisa
grade=B}}
$query="SELECT * FROM `user`";
$result=mysql_query($query);
$grades=array();
while($row=mysql_fetch_assoc($result)) {
...........
}
What I should put into the while loop?
mysql_fetch_assoc returns a associative array with the col name as the key and the value as the value. You have almost got everything you need to put this in another array how you want. Just put the following code in the loop to append the assoc arrays from each row into the $grades array:
$grades[] = $row;
Then you can access the values of the grades array like so:
$grades[1]['grade'] //returns the grade of row 1
Related
Edit:
I found a answer, so if we have to loop twice and create array inside array just do:
$data1=array_merge($data1,$result->fetch_all(MYSQLI_ASSOC));
my structure need to look like that:
['some':[{'num1':1,'num2':2},{'num1':10, 'num2':20}],'nextOne':[{'num1':1,'num2':2},{'num1':10, 'num2':20}],..... (and more...)
I have to create json data from around 4k records so i separate main data and i did something like that:
$data1 = array();
$data2 = array();
//etc.. up to 14 records
I had one existing table but i also create next with main separators, for separate data so for example:
$sql1 = "SELECT some.tit AS Title,someExtra.sub AS SubTitle FROM some INNER JOIN someExtra ORDER BY orderNr";
$result1 = $conn->query($sql1);
while($row = $result1->fetch_assoc()){
$myData = $row["Title"];
$my2ndData = $row["SubTitle "];
And now i need to find that data in next table and pass it to my array so..
$sql2 = "SELECT * FROM main WHERE expect = '$myData' AND expect2 = $my2ndData";
$result2 = $conn->query($sql2);
if($myData == 'That i need'){
$data1+=$result2->fetch_all(MYSQLI_ASSOC);
So like in example we have 2 loops, if we will have for example few records in 'someExtra' table that will match 'some' table will look next one for example : 4 times.. So if i will echo number of results inside my array just before i will send it to right array i will have 4,22,1,55,33 record in each. But on end of a script when i will count array i will heave only 33 records instead of have 115.
Also i try
array_push($data1, $result2->fetch_all(MYSQLI_ASSOC));
But that one doesynt work eather, becouse it add arrays with values to existing array... so i will have array in array in array with is not that i wont.
My final output look like that:
$outp=array('some'=>$data1,'AnextOne'=>$data2)
$myJSON = json_encode($outp, JSON_NUMERIC_CHECK);
After few hour of trying i solve the issue.
So if you will have to transfer your data from database in to json format, and separate your data by specifying keys. just do:
$data1=array_merge($data1,$result->fetch_all(MYSQLI_ASSOC));
Where:
$data1 - name of your 1st array (value for key number 1)
$result->fetch_all(MYSQLI_ASSOC) - array format where key is a row name and value is your data from row.
array_merge - that will connect your new array with old one without creating other arrays inside existing one.
So I am trying what I feel is a simply loop through a record result. My query returns one row of data. I am attempting to simply pull the value of each row from the returning record into a variable called $questions. However, my var $questions when printed out has duplicates in every space. It should read something like Bob|Ted|Joe|Sally and instead it is reading Bob|Bob|Ted|Ted|Joe|Joe|Sally|Sally. Why is the code below running twice in the foreach loop?
while ($row = mssql_fetch_array($result)){
foreach ($row as $col => $value) {
$questions.=$value."|";
}
}
echo "Questions: ".$questions."<br/>";
Here is all you need to do:
$questions = "";
while ($row = mssql_fetch_array($result)){
$questions .= $row['fieldName']."|";
}
echo "Questions: ".$questions."<br/>";
The foreach() in addition to the while() is unnecessary.
The mssql_fetch_array according to PHP doc:
In addition to storing the data in the numeric indices of the result
array, it also stores the data in associative indices, using the field
names as keys.
The result includes a normal array [0...n] with one element for each column, but also an associative array where each value is represented by a key named after the column name.
So if your first column is Id, you could get id from a row in two ways:
$id = $row[0];
# or you could do this
$id = $row['Id'];
This is why you get each value twice when looping through row.
Alright, so I'm trying to get an array of Key values from a mysql table and mysql_fetch_array() won;t seem to work properly. Code:
$x="select id from topics"
$set=mysql_query($x);
echo mysql_num_rows($set);
print_r(mysql_fetch_array($set));
$ids=mysql_fetch_array($set);
echo $ids[0];
echo $ids[1];
I've moved stuff all around but nothing seems to be changing the output:
66 //number of values in result set
Array ( [0] => 3 [id] => 3 ) //value(singular) being moved to array
4 //supposed single value of the above array
I'm really not sure what is going on here...
mysql_fetch_array brings a single row back as a PHP array, indexed by column name and by 0-based index. it DOES NOT load the whole set into a giant array, which is what you seem to be expecting.
You have to iterate over the result set in a loop, like so:
$x="select id from topics";
$set = mysql_query($x);
echo mysql_num_rows($set);
$giant_list_of_ids = array();
while ($row = mysql_fetch_array($set))
{
echo $row[0]; //or alternatively, echo $row['id'];
$giant_list_of_ids[] = $row[0];
}
I have a sql statement that returns a number of records each having 10 attributes. How can i get the data and put it into a 2D array. Currently Im doing this :
while($row1 = mysql_fetch_array($result1))
{
echo $row1[0][1]; //There is an error here
}
Is $row1 a 2D array? (It should be because Im getting n*m results). If $row1 is a 2d array then shouldn't i be able to access $row[0][1]?
mysql_fetch array returns a 1-D array, combined indexed + keyed values of one single row of data from your query results. e.g. doing
SELECT field1, field2, field3 FROM sometable
and doing a mysql_fetch_array on the result will give you an array that looks like:
$row1 = array(
0 => 'value of field1',
1 => 'value of field2',
2 => 'value of field3',
'field1' => 'value of field1'
'field2' => 'value of field2',
'field3' => 'value of field3'
)
If you want to address the entire result set as a single array, you'll have to fetch each row and add it to that array first:
$data = array();
while($row1 = mysql_fetch_array($result)) {
$data[] = $row1;
}
Which would let you do $data[0][1] (2nd field [1] of 1st record [0])
From the docs:
Fetch a result row as an associative array, a numeric array, or both
If mysql_fetch_array is recovering 1 row of data, what do you expect the second dimension to be?
First off you need to verify what sort of array you are getting, then you can understand why your keys aren't valid. Try using print_r or var_dump to examine $row1.
$row1 holds only one record at a time so it is a regular one-dimension array.
If your query returns
a b c
d e f
In the first iteration of the while loop, row1 holds a b c and in the second iteration, it holds d e f, both single-dimension arrays.
$row1 is a 1-dimensional array (a single record), in your case, holding 10 values.
As the code loops through the while statement, $row1 represents each record.
refer to the documentation here: http://ca.php.net/manual/en/function.mysql-fetch-array.php
I'm having trouble imploding a MySQL database array, below is a sample of the code I am using:
mysql_select_db($database_wlast, $wlast);
$query_category = "SELECT product_name FROM raw_materials WHERE category = '$_POST[category]'";
$category = mysql_query($query_category, $wlast) or die(mysql_error());
$row_category = mysql_fetch_array($category);
$result= implode(',',$row_category);
echo $result;
The result is the following:
Amber glass bottle 100ml, Amber glass bottle 100ml
i.e: it is spitting out the first value in the array twice and nothing else.
Please help!
while($row_category = mysql_fetch_array($category)){
$result[] = implode(',',$row_category);
}
echo implode("\n",$result);
That's how mysql_fetch_array() returns the result, twice: a numeric key and a string key. You possibly want mysql_fetch_assoc().
You want to implode all your product name from your database? if yes, than your code above is wrong.
mysql_fetch_array($category) only return 1 row of your database. If you want to implode of your all product name than code should like this:
$result="";
while($row_category=mysql_fetch_array($category))
$result=$result.",".$row[0];
or you some thing like this. You hold all your product name on array and implode it. Like this:
while($row_category=mysql_fetch_array($category))
$result[]=$row[0];
$newResult=implode(",",$result);
use mysql_fetch_array($category, MYSQL_NUM) or mysql_fetch_row($category) or mysql_fetch_assoc($category)
The problem is that by default mysql_fetch_array returns enumerated and associative arrays combined.