difficulty with Conversion of php to json file - php

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

Related

There's an excess property with my sql to json code file

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

MySQL Array Associative Only (PHP/MySQL/ADODB/JSON)

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"
}

Why json showing array index and array object multiple times in eloquent

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

PHP loop through data from JSON string

I want to get ItemCategory->id's for each Item->id
How can I do it in the best way?
Here is a part of JSON data item
"6": {
"Item": {
"id": "6",
"name": "test",
"description": "description",
},
"ItemThumbnail": null,
"ItemCategory": {
"3": {
"id": "3",
"name": "name",
"status": "active",
"date_created": "2015-07-07 11:23:52",
"date_updated": "0000-00-00 00:00:00",
},
"4": {
"id": "4",
"name": "name",
"status": "active",
"date_created": "2015-07-07 11:23:52",
"date_updated": "0000-00-00 00:00:00",
}
},
"ItemGroup": []
},
You can convert your json string to a PHP array using $array = json_decode($str, true);, then loop your array and extract the information you need.
See http://php.net/manual/en/function.json-decode.php for more detail on json_decode
After json decode use this.
foreach($Item as $Items)
{
$ItemCategory = $Items->ItemCategory;
foreach($ItemCategory as $ItemCategorys)
{
echo $ItemCategorys->id;
}
}

mongodb projection matching sub array

I am new to mongodb.I have a mongodb document whose structure is like this :
"prices": {
"0": {
"Quantity": {
"1": "10",
"2": "14",
"5": "16",
},
"option1": "a4",
"option2": "50",
"option3": "qwe",
"option4": "sdslk"
},
"1": {
"Quantity": {
"1": "20",
"2": "12",
"10": "3",
},
"option1": "a5",
"option2": "100",
"option3": "kl",
"option4": "oiuio"
},
"2": {
"Quantity": {
"10": "20",
"15": "12",
"100": "3",
},
"option1": "a2",
"option2": "10",
"option3": "kadl",
"option4": "qwqw"
},
...
}
I have an array in PHP like this
Array => ("option1" => "a2",
"option2"=> "10",
"option3"=> "kadl",
"option4"=> "qwqw"
)
I want to query the document such that it matches a particular sub array in the mongodb doc and return the following result
"2": {
"Quantity": {
"10": "20",
"15": "12",
"100": "3",
},
"option1": "a2",
"option2": "10",
"option3": "kadl",
"option4": "qwqw"
}
But only when all the correspoding values (except Quantity match).
The options (option1, option2, ...) are different for different documents.
Please comment is question is not clear
Not possible since you don't know the name of the key to match to. Perhaps you want prices to actually be an array?
{ "prices" : [{
"Quantity": {
"10": "20",
"15": "12",
"100": "3",
},
"option1": "a2",
"option2": "10",
"option3": "kadl",
"option4": "qwqw"
}] }
Then you can get what you want like this:
db.test.find({ "prices" : { "$elemMatch" : { "option1" : "a2", "option2" : "10", "option3" : "kadl", "option4" : "qwqw" } } }, { "prices.$" : 1 })

Categories