JSON output using PHP - php

HI i need to output a JSON object for consuming in iphone
i am able to output like
{"feed":{"id":"1","player":"player1"}}
{"feed":{"id":"1","player":"player2"}}
{"feed":{"id":"2","player":"player3"}}
The code :
$query = "SELECT id,player FROM MyVideos";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
$players[] = array();
if(mysql_num_rows($result)){
while($player = mysql_fetch_assoc($result)){
$players[] = array('player'=>$player);
echo json_encode(array("feed"=>$player));
}
}
But i need to output some thing like this
{"feed":
{"id":"1","player":"player1"},
{"id":"1","player":"player2"},
{"id":"2","player":"player3"}
}
Can anyone please help me with this.
Thanks,

The output you posted isn't valid JSON, you need to put brackets around the items in feed:
{"feed": [
{"id":"1","player":"player1"},
{"id":"1","player":"player2"},
{"id":"2","player":"player3"}
]}
You should loop through your results and build an array of your feed items, and then output it all at once, like this:
$feed_items = array();
if (mysql_num_rows($result)) {
while ($player = mysql_fetch_assoc($result)){
$feed_items[] = $player;
}
}
echo json_encode(array("feed" => $feed_items));

Related

how to make a mysql query and turn it into an array in php

if I have a query that looks like this in php...
if ($result3 = $connuser->query("SELECT * FROM devices WHERE uid='".$username."'")){
}
How can I turn $result3 into an array?
Also, separate question, I tried doing
echo $result3;
but it doesn't work. Is there a way to view $result3 so I can see what it looks like?
You can do:
$rows = [];
while($row = mysqli_fetch_array($result3))
{
$rows[] = $row;
}

Fetching data from Oracle database and store into an array using PHP

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);

JSON and array structure typeahead

I am trying to create a json file from an sql query, and search this json using twitter typeahead. However the json format doesn't look correct.
The json needs to be in a certain format for typeahead like below;
['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California' ...];
However my json is in the following format;
["{\"title\":\"Item 1\"}","{\"title\":\"Item 2\"}","{\"title\":\"Item 3\"}"
Newbie to php/sql/json I'm sure there is something really obvious I'm missing or doing wrong. Maybe I should be using a foreach and not while? I am able to echo out the $titles so I now the query is working.
If somebody cold point me in the right direction I would appreciate it.
My code so far;
$sql = ("SELECT title FROM publication");
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
$data = array();
while($row = $result->fetch_assoc()){
$data[] = json_encode($row);
$titles = json_encode($data);
echo $titles;//for testing
}
file_put_contents('titles.json', $titles);
You're over-encoding your data and
you're not including the data you actually want.
Put the data you want into the array and JSON-encode the whole thing only at the end:
while ($row = $result->fetch_assoc()) {
$data[] = $row['title'];
}
file_put_contents('titles.json', json_encode($data));
You are performing json_encode twice which should not be the case.
Instead the Code should be like below:
$data[] = $row;
$titles = json_encode($data);
or simply
$titles = json_encode($row);
Use json_encode once
$data[] = $row; /*$data[] = json_encode($row);*/
and write this:
$titles = json_encode($data);
OR
$titles = json_encode(array_values($data)); /*You may need to use this to get exact output*/
After while loop
You are inserting associative array to your json array('title'=>'sometitle') but you only need that title.
The solution is to only save the title value from the db resulting row to the array:
while($row = $result->fetch_assoc()){
$data[] = $row['title'];
echo json_encode($data); // dont encode twice
}
file_put_contents('titles.json', json_encode($data));

Fetch JSONObject instead of JSONArray for single row of data in MySQL Database using PHP

I have a few queries that I'm executing in my Android app. Each will only return one row of data, so I'd like to treat the output as JSONObjects rather than JSONArrays since they would be just be arrays with single objects inside; kind of pointless in my view.
As of now my PHP looks like this:
$query = "SELECT moveCount FROM Chessmates.Board_States WHERE Games_GameID = 2;";
$sth = mysqli_query($con, $query);
if(mysqli_errno()) {
echo "error";
} else {
$rows = array();
while($r = mysqli_fetch_array($sth, MYSQLI_ASSOC)) {
$rows[] = $r;
//var_dump($r);
}
echo json_encode($rows);
}
and the output looks like this:
[{"moveCount":"0"}]
I'd like it to look like this:
{"moveCount":"0"}
If it's only returning one row of data, then you don't need to make an array.
while($r = mysqli_fetch_array($sth, MYSQLI_ASSOC)) {
$rows = $r;
}
You get the data as you did because you were creating an array of objects, But since you're only going to ever get one result (as you stated), you can simple set it up as a variable.

Building a Json array with multiple parameters in Php

I managed to output the column 'resort' in the Json array, but I need 'country' too, as well as 'aantal'. Have no idea how to do that. Can someone please help me?
if ($numrows < 1 && strlen($sq) > 3)
{
$sql = "SELECT resort, country, COUNT(image) AS aantal FROM sx_cam
LEFT JOIN sv_orte ON sv_cam.res_id = sv_orte.res_id
WHERE sound=soundex('$sq') and (status < 1) GROUP BY resort order by aantal desc";
$result2 = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($result2);
$suggest = 2;
}
$items = array();
while($row = mysql_fetch_assoc($result2)){
$items[$row['resort']] = $row['resort'];
}
foreach ($items as $key=>$value) {
echo strtolower($key)."|$value\n";
}
You're building the array the wrong way. Once you get the array right, it is as simple as making a call to json_encode
I'm not entirely sure how you want your json to look, but something like this should get you started
$items = array();
while($row = mysql_fetch_assoc($result2)){
//first we build an 'object' of the current result
$item['country'] = $row['country'];
$item['resort'] = $row['resort'];
//now push it on the array of results
$items[] = $item;
}
echo json_encode($items);
Once you get the above code working, you can tweak the PHP array to change the structure of the JSON to suit your needs.

Categories