I have an sql query which is fetching the data in format :
Date comment
12-3-2016 local meeting
and so-on..
I am using array_push for values so that i can display them in a page but i am only getting one value value from using array_push. here is my code:
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"Date"=>$res['date'],
"Events"=>$res['comment']
)
);
//using JSON
echo json_encode(array("result"=>$result));
mysqli_close($con);
The output which i am getting is like this:
{"result": [{"Date": "12-3-2016","Events": "Parent Teacher Meeting"}]}
mysqli_fetch_array($r) fetches one row from a result set. To get all rows from a result set, you should use while-loop:
$result = array();
while($res = mysqli_fetch_array($r)) { // here, a while-loop
array_push($result,array(
"Date" => $res['date'],
"Events" => $res['comment']
));
}
//using JSON
echo json_encode(array("result"=>$result));
mysqli_close($con);
Related
I am trying to fetch the list of id's associated with mobile number using following code.
$sql = "SELECT id FROM complaints WHERE mobile=1555521555";
$r = mysqli_query($conn,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"id"=>$res['id']
)
);
header('Content-Type:application/json');
echo json_encode(array("result"=>$result));
mysqli_close($conn);
But it only result me a single id every time as below instead of list of id's
{"result":[{"id":"82925318"}]}
Actual query result is -
How do I get the multiple results in array of arrays format or in multidimensional array? Like below -
{"result":[{"id":"82925318"}, {"id":"82925319"}]}
You need loop to get all data
$result = array();
while($res = mysqli_fetch_array($r))
{
array_push($result,array(
"id"=>$res['id']
)
);
}
I have two different queries which I have to append in same array in form of JSON..
Here is my code from 1st query...
while($row = mysqli_fetch_array($res)) {
array_push($result,array('name'=>$row[0],'photo'=>$row[1],'rollno'=>$row[2],'id'=>$row[3]));
}
Here is my second query push similar as first one.. number of rows is always same as above query
array_push($result,array('status'=>'$status');
After that I'm encoding them like this
echo json_encode(array("result"=>$result));
Here is what I am getting
{"result":[{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26"},
{"status":"status"}]
But I want to result like this
{"result":[{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26","status":"status"}]
I mean status will merge into my every node... how can I achieve this..?
Try the below to add status field to each array:
while($row = mysqli_fetch_array($res)){
array_push($result,array('name'=>$row[0],'photo'=>$row[1],'rollno'=>$row[2],'id'=>$row[3]));
}
$res_row = 0;
while($row2 = mysqli_fetch_array($res2)){
$status = $row2[0]; // status value here
$result[$res_row]['status']=$status;
$res_row++;
}
echo json_encode(array("result"=>$result));
Try this please, using temporary arrays that are merged after all your queries are complete:
// Query 1
while($row = mysqli_fetch_array($res)){
$tmpResults[] = $row;
}
// Query 2
$tmpResult2 = array('status'=>'$status');
// Merge Everything
$final = array_merge($tmpResults, $tmpResult2);
// Encode
$json = json_encode($final, TRUE);
Good luck
I'm trying to fetch data from database, date-with-time(timestamp) and values(number) , and storing into an array using php.
here is my data in database as follows=
WD DT
25-FEB-15 12.14.00.000000 AM 15.739993
25-FEB-15 12.23.00.000000 AM 13.698263
25-FEB-15 12.43.00.000000 AM 13.214383
fetch.php
<?php
include("md.php");
$sql = "SELECT * from datatable";
$result =oci_parse($conn, $sql);
$r=oci_execute($result);
$arr = array();
$row=oci_num_rows($stid);
$arr[0]=array('wd','dt');
for($i=1; $i<($row+1); $i++)
{
$arr[$i]= array(substr(oci_result($result, $i-1, "wd"),0,18),(float)oci_result($result,$i-1,"dt"));
//$arr[$i]= array(substr(oci_result($result, $i-1, "wd"),0,18),(int)oci_result($result,$i-1,"dt"));
}
echo json_encode($arr);
//print_r($arr);
?>
$arr getting following output:
[["WD"],["DT"]]
Q1. Why am i not getting rest of data? where am i doing wrong?
but if i use
while($row = oci_fetch_row($stid)){
$arr[] = $row;
}
if i use json_encode then =
[["25-FEB-15 12.14.00.000000 AM","15.739993"],["25-FEB-15 12.23.00.000000 AM","13.698263"],["25-FEB-15 12.43.00.000000 AM","13.214383"],....
if i use
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = $row;
}
if i use json_encode then =
[{"WD":"25-FEB-15 12.14.00.000000 AM","DT":"15.739993"},{"WD":"25-FEB-15 12.23.00.000000 AM","DT":"13.698263"},........]
I want the output as follows=
[["25-FEB-15 12.14.00 AM",15.739993],["25-FEB-15 12.23.00 AM",13.698263],["25-FEB-15 12.43.00 AM",13.214383],....]
Q2. How can i get it?
please help
Because the data is being returned in an associative array you get the column name and the data for each column in each row returned to you. So this is being returned in $row
"WD" => "25-FEB-15 12.14.00.000000 AM", "DT" => "15.739993"
All you need to do is pick out the data from each row and ignore the Key like so :-
$arr = array();
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = array($row['WD'] , $row['DT']);
}
echo json_encode($arr);
I've got a database with 5 columns and multiple rows. I want to fetch the first 3 rows and echo them as an array. So far I can only get the first row (I'm new to PHP and mysql). Here's my PHP so far:
//==== FETCH DATA
$result = mysql_query("SELECT * FROM $tableName");
$array = mysql_fetch_row($result);
//==== ECHO AS JSON
echo json_encode($array);
Help would be much appreciated.
You need to loop through the results. mysql_fetch_row gets them one at a time.
http://php.net/manual/en/function.mysql-fetch-row.php
The code would end up like:
$jsonData = array();
while ($array = mysql_fetch_row($result)) {
$jsonData[] = $array;
}
echo json_encode($jsonData);
//json_encode()
PLEASE NOTE
The mysql extension is deprecated in PHP 5.5, as stated in the comments you should use mysqli or PDO. You would just substitute mysqli_fetch_row in the code above.
http://www.php.net/manual/en/mysqli-result.fetch-row.php
I do like this while quering an ODBC database connection with PHP 5.5.7, the results will be in JSON format:
$conn = odbc_connect($odbc_name, 'user', 'pass');
$result = odbc_exec($conn, $sql_query);
Fetching results allowing edit on fields:
while( $row = odbc_fetch_array($result) ) {
$json['field_1'] = $row['field_1'];
$json['field_2'] = $row['field_2'];
$json['field_3'] = $row['field_1'] + $row['field_2'];
array_push($response, $json);
}
Or if i do not want to change anything i could simplify like this:
while ($array = odbc_fetch_array($result)) { $response[] = $array; }
What if i want to return the results in JSON format?, easy:
echo json_encode($response, true);
You can change odbc_fetch_array for mysqli_fetch_array to query a MySql db.
According to the PHP Documentation mysql_fetch_row (besides that it's deprecated and you should use mysqli or PDO)
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
so you need for example a while loop to fetch all rows:
$rows = array();
while ($row = mysql_fetch_row($result)) {
$rows[] = $row;
}
echo json_encode($rows);
I leave it to you how to only fetch 3 rows :)
You need to put this in some kind of a loop, mysql_fetch_row returns results one at a time.
See example:
http://www.php.net/manual/en/mysqli-result.fetch-row.php#example-1794
$result = mysql_query( "SELECT * FROM $tableName ORDER BY id LIMIT 3");
$json = array();
while($array = mysql_fetch_row($result)){
$json[] = $array;
}
echo json_encode($json);
How can i get every row of a mysql table and put it in a php array? Do i need a multidimensional array for this? The purpose of all this is to display some points on a google map later on.
You need to get all the data that you want from the table. Something like this would work:
$SQLCommand = "SELECT someFieldName FROM yourTableName";
This line goes into your table and gets the data in 'someFieldName' from your table. You can add more field names where 'someFieldName' if you want to get more than one column.
$result = mysql_query($SQLCommand); // This line executes the MySQL query that you typed above
$yourArray = array(); // make a new array to hold all your data
$index = 0;
while($row = mysql_fetch_assoc($result)){ // loop to store the data in an associative array.
$yourArray[$index] = $row;
$index++;
}
The above loop goes through each row and stores it as an element in the new array you had made. Then you can do whatever you want with that info, like print it out to the screen:
echo $row[theRowYouWant][someFieldName];
So if $theRowYouWant is equal to 4, it would be the data(in this case, 'someFieldName') from the 5th row(remember, rows start at 0!).
$sql = "SELECT field1, field2, field3, .... FROM sometable";
$result = mysql_query($sql) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
echo $array[1]['field2']; // display field2 value from 2nd row of result set.
The other answers do work - however OP asked for all rows and if ALL fields are wanted as well it would much nicer to leave it generic instead of having to update the php when the database changes
$query="SELECT * FROM table_name";
Also to this point returning the data can be left generic too - I really like the JSON format as it will dynamically update, and can be easily extracted from any source.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo json_encode($row);
}
You can do it without a loop. Just use the fetch_all command
$sql = 'SELECT someFieldName FROM yourTableName';
$result = $db->query($sql);
$allRows = $result->fetch_all();
HERE IS YOUR CODE, USE IT. IT IS TESTED.
$select=" YOUR SQL QUERY GOOES HERE";
$queryResult= mysql_query($select);
//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();
//STORE ALL THE RECORD SETS IN THAT ARRAY
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC))
{
array_push($data_array,$row);
}
mysql_free_result($queryResult);
//TEST TO SEE THE RESULT OF THE ARRAY
echo '<pre>';
print_r($data_array);
echo '</pre>';
THANKS