guys help me I'm a bit confused how my code producing an extra property to my json result see below:
$connect=mysqli_connect("localhost", "root", "", "thecmlco_widget");
$query = "SELECT * FROM widgetdb";
$result = mysqli_query($connect,$query);
$rows = array();
while($r = mysqli_fetch_array($result)) {
$rows[] = $r;
}
echo json_encode($rows);
mysqli_close($connect);
here is the result with an extra property 0 , 1 , 2 , 3 , 4 ,5 and I don't know why its creating those extra property.
[
{
"0": "1",
"1": "Vacation",
"2": "http://trilogy.editor.multiscreensite.com/preview/dm-theme-1000772-en-291",
"3": "https://dd-cdn.multiscreensite.com/themes-panel/preview/vacation.jpg",
"4": "1000772",
"5": "0",
"id": "1",
"template_name": "Vacation",
"preview_url": "http://trilogy.editor.multiscreensite.com/preview/dm-theme-1000772-en-291",
"thumbnail_url": "https://dd-cdn.multiscreensite.com/themes-panel/preview/vacation.jpg",
"templade_id": "1000772",
"can_build_from_url": "0"
},
{
"0": "2",
"1": "Product",
"2": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"3": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"4": "1003040",
"5": "0",
"id": "2",
"template_name": "Product",
"preview_url": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"thumbnail_url": "https://irp-cdn.multiscreensite.com/ce1f372c/siteTemplateIcons/Mstzqt8GTRSxzCt6QTue_BigPreview_iotech.png",
"templade_id": "1003040",
"can_build_from_url": "0"
}
]
Use mysqli_fetch_assoc() instead of mysqli_fetch_array() function to get rows as associative array
Related
This is my PHP code for json output:
$sql="SELECT id,name FROM languages ORDER BY id";
$result=mysqli_query($conn,$sql);
// Fetch all
$result = mysqli_fetch_all($result,MYSQLI_ASSOC);
$out_put = json_encode($result);
echo $out_put;
This is the json output of the above php code:
{
"0": {
"id": "1",
"name": "English"
},
"1": {
"id": "2",
"name": "Kanada"
},
"2": {
"id": "3",
"name": "Hindi"
},
"3": {
"id": "4",
"name": "Telugu"
}
}
But I want output like this:
{
"Responsecode":200,
"Message":"Sucess",
"languagelist": [
{
"id": "1",
"name": "English"
},
{
"id": "2",
"name": "Kannada"
},
{
"id": "3",
"name": "Hindi"
},
{
"id": "4",
"name": "Telugu"
}
]
}
I am trying to create API and I am new in it. Please help. Thank you in advance.
Just write
$out_put = json_encode([
"Responsecode" => 200,
"Message" => "Sucess",
"languagelist" => $result
]);
You can do it by this way:
$output['Responsecode'] = 200;
$output['Message'] = "Sucess";
$output['languagelist'] = $result;
$out_put = json_encode($output);
I have been tiring to convert the PHP to JSON file but the JSON file generated is very different then normally occur.
The table is present below
rid rname mobile email address opentiming closetiming menuid type averagecost image
1 abc 9876543212 sbjaca#gmail.com fdsjdsfdnm 00:10:00 00:00:00 1 asian 120 http://gjsblog.esy.es/images/download.png
2 abcdefc 9876543212 ajit#gmail.com qwertym 00:00:03 00:00:04 2 chinese 120 http://gjsblog.esy.es/images/The_Table.png
The php file is present below
//retrieve.php
<?php
include("dbconfig.php");
$result = #mysql_query("select * from Restaurants ");
$response =array();
if(#mysql_num_rows($result)>0){
$response['Restaurants'] = array();
while($row=#mysql_fetch_array($result)){
array_push($response['Restaurants'], $row);
}
}
if($result){
$response['success']=1;
$response['message']="Records Retrieved sucessfully";
}else{
$response['success']=0;
$response['message']="Retrieval Failure";
}
echo json_encode($response);
?>
The JSON contains the data in the column twice, once before the column name and once after the column name. The JSON appears as
{
"Restaurants": [
{
"0": "1",
"rid": "1",
"1": "abc",
"rname": "abc",
"2": "9876543212",
"mobile": "9876543212",
"3": "sbjaca#gmail.com",
"email": "sbjaca#gmail.com",
"4": "fdsjdsfdnm",
"address": "fdsjdsfdnm",
"5": "00:10:00",
"opentiming": "00:10:00",
"6": "00:00:00",
"closetiming": "00:00:00",
"7": "1",
"menuid": "1",
"8": "asian",
"type": "asian",
"9": "120",
"averagecost": "120",
"10": "http:\/\/gjsblog.esy.es\/images\/download.png",
"image": "http:\/\/gjsblog.esy.es\/images\/download.png"
},
{
"0": "2",
"rid": "2",
"1": "abcdefc",
"rname": "abcdefc",
"2": "9876543212",
"mobile": "9876543212",
"3": "sbjaca#gmail.com",
"email": "sbjaca#gmail.com",
"4": "fdsjdsfdnm",
"address": "fdsjdsfdnm",
"5": "00:00:03",
"opentiming": "00:00:03",
"6": "00:00:04",
"closetiming": "00:00:04",
"7": "2",
"menuid": "2",
"8": "chinese",
"type": "chinese",
"9": "120",
"averagecost": "120",
"10": "http:\/\/gjsblog.esy.es\/images\/The_Table_(restaurant)_logo.png",
"image": "http:\/\/gjsblog.esy.es\/images\/The_Table_(restaurant)_logo.png"
}
],
"success": 1,
"message": "Records Retrieved sucessfully"
}
Change
while($row=#mysql_fetch_array($result))
To
while($row=#mysql_fetch_array($result, MYSQL_ASSOC))
Because if you just use the first statement, MYSQL_BOTH will be used as default, and make the result array like that.
Just a suggestion, you better use MySQLi or PDO_MySQL, because mysql_fetch_array has been deprecated in PHP 5.5.0 and removed in PHP 7.0.0
I'm using PHP+MySQL+ADODB to fetch an array from the database and then convert it to JSON. However, the object ends up with duplicate entries (associative and numeric).
How would I go about returning only the associative rows?
$sql = 'SELECT * FROM table';
$rs = $conn->execute($sql);
$result = $rs->getrows();
echo json_encode($result);
// OUTPUT
{
"0": "18556",
"VID": "18556",
"1": "1",
"UID": "1",
"2": "Title of entry",
"title": "Title of entry",
"3": "0",
"likes": "0",
"4": "0",
"dislikes": "0"
}
My JSON output is like:
{
"0": "1",
"1": "araer",
"2": "aeraer",
"3": "aeraer",
"4": "News/Magzine Website",
"5": "2016-01-22 13:15:56",
"6": "2016-01-22 13:15:56",
"id": "1",
"name": "araer",
"email": "aeraer",
"url": "aeraer",
"web": "News/Magzine Website",
"created_at": "2016-01-22 13:15:56",
"updated_at": "2016-01-22 13:15:56"
}, {
"0": "2",
"1": "asd",
"2": "asd",
"3": "sfd",
"4": "sdf",
"5": "2016-02-10 13:06:28",
"6": "0000-00-00 00:00:00",
"id": "2",
"name": "asd",
"email": "asd",
"url": "sfd",
"web": "sdf",
"created_at": "2016-02-10 13:06:28",
"updated_at": "-0001-11-30 00:00:00"
}
The code I am using in Model:
public function getBlog() {
try {
$result = $this - > get();
return $result;
} catch (Exception $ex) {
return array();
}
return array();
}
Check PDO fetch style in your config/databse.php array. If it is not present there then it will use PDO::FETCH_BOTH (by default) which returns both associative and numeric values. To get only associative value you need to set 'fetch' => PDO::FETCH_CLASS, or 'fetch' => PDO::FETCH_ASSOC
You function will retutn all records from your blog table.
And if you want to get single record then try something like
$this->get()->first();
I am trying to generate a JSON output from the DATABASE with MySQL.
The result that I want is that I want an array around two matching ID's found in the tabel in the database.
To visualise what I wish to achieve here is my code:
This is my query
$sql = "SELECT * FROM `flower_garden` WHERE `id_flower` IN (0, 1)";
$result = mysql_query($sql);
while($record = mysql_fetch_assoc($result)) {
$rows[] = $record;
}
print json_encode($rows);
This is the JSON result I wish to achieve:
(What I want)
[
[
"id": "1",
"id_flower": "3",
"Title": "rose",
"Price": 1.25,
"Number": 15
},
{
"id": "2",
"id_flower": "3",
"Title": "daisy",
"Price": 0.75,
"Number": 25
}
],
[
{
"id": "3",
"id_flower": "6",
"Title": "rose",
"Price": 1.25,
"Number": 15
},
{
"id": "4",
"id_flower": "6",
"Title": "daisy",
"Price": 0.75,
"Number": 25
}
]
]
Visual result:
So, I want the matching ID's (in this case id_flower) put in one array.
This is the result I get:
(What I get)
[
[
"id": "1",
"id_flower": "3",
"Title": "rose",
"Price": 1.25,
"Number": 15
},
{
"id": "2",
"id_flower": "3",
"Title": "daisy",
"Price": 0.75,
"Number": 25
},
{
"id": "3",
"id_flower": "6",
"Title": "rose",
"Price": 1.25,
"Number": 15
},
{
"id": "4",
"id_flower": "6",
"Title": "daisy",
"Price": 0.75,
"Number": 25
}
]
]
Visual result:
Try this
foreach ($rows as $key => $val) {
$return[$val['id_flower']][] = $val;
}
echo json_encode($return);
Please note I have not tested it.
$sql = "SELECT * FROM `flower_garden` WHERE `id_flower` IN (0, 1)";
$result = mysql_query($sql);
$rows = array();
while($record = mysql_fetch_assoc($result)) {
array_push($rows[$record["id_flower"]], $record);
}
$result = array();
foreach($rows as $k => $v){
array_push($result, $v);
}
echo json_encode($result);
test yourself !