Efficient way to pass JSON from PHP - php

Here is a JSONArray I have:
[{"items_id":"13","total":"1"}, {"items_id":"216","total":"0"},{"items_id":"16","total":"1"}]
Sometimes, each object has more than two properties (attributes?). But I am just showing the principle here. In Java, I only need to grab "total". I don't need "items_id".
I assume it shows up because here is my MySQL query in PHP:
$count_query_result=mysql_query("
SELECT items.items_id,
COUNT(ratings.item_id) AS total
FROM `items`
LEFT JOIN ratings ON (ratings.item_id = items.items_id)
WHERE items.cat_id = '{$cat_id}' AND items.spam < 5
GROUP BY items_id ORDER BY TRIM(LEADING 'The ' FROM items.item) ASC;");
Here is my JSON output (I have only displayed one of three queries above):
print(json_encode(array($output,$output2,$output3)));
I only want three properties encoded in JSON (one in each of the three output variables). I want the properties "total", "rate" ,and "item".
So my question is, can I get rid of the unneeded items_id property? Or do I even NEED to? (I know I need it in the SQL to make the query work -- but how can I remove it in the JSONArray?)
I am thinking if I have a list with hundreds or thousands of items, I can save half the space (and time?) by only outputing the JSON property I need -- is this thinking correct?
Edit: More code as requested:
while($row=mysql_fetch_assoc($count_query_result))
$output[]=$row;
while($row=mysql_fetch_assoc($average_query_result))
$output2[]=$row;
while($row=mysql_fetch_assoc($items_query_result))
$output3[]=$row;
print(json_encode(array($output,$output2,$output3)));
mysql_close();

Use the JSONObject to parse the String. Just put the string as an parameter in the constructor of the JSONObject.
jObject = new JSONObject(myString);
After you can retrieve all variables out of the JSONObject.
For the php side:
Use json_decode end json_encode to switch betweeen json and mixed variable.
Once you have you mixed variable, delete the stuff you don't need and convert it back to json.

http://php.net/unset
Unset the unneeded values, then output json

I don't think you need it in your SQL query. If you need data from a database just in the context of the query, you don't need to SELECT it. Try doing just the:
SELECT COUNT(*) FROM ...

Related

JSON data handling with mySQL and php

For the past 2 days I have been looking over the internet on how to handle data stored as json in mySQL database.All I found was a single article in here which I followed with no luck.So here is my question
This is my table called additional with 2 columns only...jobid and costs. jobid is an int of length 5 and obviously the primary key, costs is simply stored as text. Reason I combined all the costs under one column is because the user in my application can put whatever he/she wants in there, so to me the costs is/are unknown. For example one entry could be
24321 , {"telephone" : "$20"}
or
24322 , {"telephone" : "$20", "hotel" : "$400"}
and so on and so forth but I hope you get the point.
Now given this example I need to know how to handle data in and out from the database stored as json using php. So insert, select and update but I think with one given example I can do the rest If someone can help me understand how to handle json data in and out from a database.
Oh and one last thing. Not only I need to know how to fetch the data I need to be able to separate it too e.g:
$cost1 = {"telephone" : "$20"};
$cost2 = {"hotel" : "$400"};
I really hope someone can help with this because like I said above I spent 2 days trying to get my head around this but either no articles on this matter(except the one from this site) or completely irrelevant to my example
You tagged it as PHP so you can use php functions: json_encode and json_decode.
For example when you read (SELECT) and got this cost value in string corresponding to the primary key 24322:
//after you query db and got the cost in string...
$sql = "SELECT * FROM additional";
$result = mysqli_query($conn,$sql); $row = mysqli_fetch_array($result);
//from your comment below.... just changed to $cost so I don't have to change everything here...
$cost = $row['costs'];
//$cost = '{"telephone" : "$20", "hotel" : "$400"}'
//you just have to:
$cost = json_decode($cost);
// result in an object which you can manipulate such as:
print_r($cost->telephone);
// $20 or:
print_r($cost->hotel);
//$400;
//or if you want to go through all of the costs... you change that to array:
$cost = (array)$cost; //or on your json_decode you add a TRUE param... ie(json_decode($cost, TRUE))...
print_r($cost);
//will produce an associative array: ['telephone'=>'$20', 'hotel'=>'$400']
//for which you can do a foreach if you want to go through each value...
On the other hand when you save to db with an object:
$cost = (object)['hotel'=>'$300', 'taxi'=>'$14'];
//you json_encode this so you can write to db:
$cost = json_encode($cost);
//a string... you can then use $cost to write to db with (insert, update, etc)
Note: json_decode needs the input string to be UTF-8 encoded. So you might need to force your mysql server to provide UTF-8. Some reading: https://www.toptal.com/php/a-utf-8-primer-for-php-and-mysql
Hope this helps...
You can use json_encode() and json_decode() throughout your update or insert process.
Basically
json_encode() takes Array and returns JSON as String
json_decode() takes JSON as String and returns Array
http://php.net/manual/en/function.json-encode.php
So in your case whenever you want to update 24321 , {"telephone" : "$20"}
you got to decode like
$array = json_decode($row['jsonDataOrWhateverTheColumnNameIs'],true);
$array['telephone'] = "40$";
...
...
$jsonString = json_encode($array); // Use this string with your update query.

Drupal / MySQL fetchAllAssoc(); resulting in exception

I have an external database that I am trying to access from within a Drupal page, I have successfully queried the database and output data to the page using fetchAssoc(), however this only returns the first row in the database. I would like to return all rows into an array for processing, so I'm attempting to use fetchAllAssoc(), this however results in an exception. The database has the following SQL fields:
id, model, manufacturer, url, date_modified
My test code is as follows:
<?php
db_set_active('product_db');
$query = db_select('product', 'p')->fields('p');
$sqlresults = $query->execute()->fetchAllAssoc('id');
foreach($sqlresults as $sqlresult)
{
printf($sqlresult);
}
db_set_active();
?>
I'm thinking that it is the key field 'id' that I am specifying with fetchAllAssoc() that is the problem, as fetchAssoc() prints values correctly. All documentation I have found seems to say that you pass a database field as the key but I have also passed a numeric value with no success.
Many thanks in advance for any advice, I'm sure I'm just missing something stupid.
I think it should work in this way, but within the foreach you want to print the $sqlresult variable as a string, but it is an object (it causes the error).
printf function needs a string as the first parameter, see:
http://php.net/manual/en/function.printf.php
Use for instance var_dump instead:
var_dump($sqlresult);

Filtering a query in parse that the key is a pointer

Im a bit new to the php side of parse, mainly objective-c and swift but I need to write some code that I can query a column (not the objectID one) to return the results..
The column I'm trying to query is a pointer to another class.
Here is the very basic code I have which returns all the rows in the class and the pointers data with the include key, but I need to filter or get only the row/s that I'm looking for.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$results = $query->find();
In the php sdk I see an option to use equalTo which has a key and a value to it so I tried the following code.
so I choose the column that was the pointer , and its objectid to hopefully only return those row/s that has that object id.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$query->equalTo("ColumnNameX", "yjdyaGRWP7");
$results = $query->find();
Nothing was returned and a php error was spit out
'pointer field ColumnNameX needs a pointer value' in /var/www/parse/src/Parse/ParseClient.php:326
So im not 100% sure why I cant filter by a ColumnNameX using its objectID which is a pointer to ClassA..
Did I miss something in the PHP docs..
I mean ideally in mysql to just get that row I want would be
SELECT * FROM ClassB WHERE ColunNameX = yjdyaGRWP7
That would return me the row of data, I can use a Join of course to get some info from ClassA as well.
Any thoughts on what im missing or do I need to first query the Class A to get a pointer, then in the equalTo do something like ("ColumnNamX" , $pointerfromClassA) ?
any one have anyone point out what im missing or have a code example.. I have seen some that use the objectID but I dont have access to that.
Ok I figured out one way to do this, not sure if this is the right way but it returns now what I want..
$query->equalTo("ColunNameX", array("__type" => "Pointer", "className" => "ColunNameX", "objectId" => "yjdyaGRWP7"));

Unserialize values from mySQL

I am using a classified scripts and saves user_meta data in the wp_usermeta table.
The meta_key field is called user_address_info and in it there are all the data like below :
s:204:"a:7:{s:9:"user_add1";s:10:"my address";s:9:"user_add2";N;s:9:"user_city";s:7:"my city";s:10:"user_state";s:8:"my phone";s:12:"user_country";N;s:15:"user_postalcode";s:10:"comp phone";s:10:"user_phone";N;}";
I am not using all the fields on the script but user_add1, user_city, user_state and user_postalcode
I am having trouble to get the data using SQL like the example below (wordpress) :
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);
I would like some help here so that I will display anywhere (I dont mind using any kind of SQL queries) the requested info e.g. the user_city of current author ID (e.g. 25)
I was given the following example but I want something dynamic
<?php
$s = 's:204:"a:7:{s:9:"user_add1";s:10:"my address";s:9:"user_add2";N;s:9:"user_city";s:7:"my city";s:10:"user_state";s:8:"my phone";s:12:"user_country";N;s:15:"user_postalcode";s:10:"comp phone";s:10:"user_phone";N;}"';
$u = unserialize($s);
$u2 = unserialize($u);
foreach ($u2 as $key => $value) {
echo "<br />$key == $value";
}
?>
Thank you very much.
No, you can't use SQL to unserialize.
That's why storing serialized data in a database is a very bad idea
And twice as bad is doing serialize twice.
So, you've got nothing but use the code you've given.
I see not much static in it though.
do you experience any certain problem with it?
Or you just want to fix something but don't know what something to fix? Get rid of serialization then
i have found that the serialize value stored to database is converted to some other way format. Since the serialize data store quotes marks, semicolon, culry bracket, the mysql need to be save on its own, So it automatically putting "backslash()" that comes from gpc_magic_quotes (CMIIW). So if you store a serialize data and you wanted to used it, in the interface you should used html_entity_decode() to make sure you have the actual format read by PHP.
here was my sample:
$ser = $data->serialization; // assume it is the serialization data from database
$arr_ser = unserialize(html_entity_decode($ser));
nb : i've try it and it works and be sure avoid this type to be stored in tables (to risky). this way can solve the json format stored in table too.

what's the most easy-to-parse format for PHP?

In my PHP file,I'm reading out bulk of information using query like below:
SELECT GROUP_CONCAT(CONCAT('<comment><body><![CDATA[',body,']]></body>','<replier>',if(screen_name is not null and !anonymous,screen_name,''),'</replier>','<created>',created,'</created></comment>') SEPARATOR '')
FROM idiscussion
LEFT JOIN
users
ON idiscussion.uid=users.id
WHERE idiscussion.iask_id = 1
yes,it's like xml format,but seems it's not easiest to parse in PHP,
Any recommendation on which format to use in order to parse easily later on?
AS for my I need to concatenating them all together,
You can think about this case: To retrieve 1 topic and discussions under this topic at a time. 1 topic corresponds to multiple discussions.How can you do it in 1 query without concatenating it together?
If you're getting data from a SQL database, it's much easier not to use SQL to clobber it into some text format, but to instead perform a regular query and use the SQL functions to work with the results that you're given.
If you end up needing to serialize data to other PHP clients, I would recommend PHP's standard serialization format.
Depends on the data structure, but a list of values may easily be saved as a semi-colon separated list of entries using join(), and then easily parsed using split().
This is extremely simple, but may not be ideal for slightly more complex data.
Answer to comment: if the data contains a semi-colon, it needs to be escaped as appropriate.
You could use DOM or SimpleXML to parse XML files, but also JSON or YAML. All are quite easy to use as you can rely on high-level parsers for PHP. They have advantages and disadvantages depending on how you want to use the format.
Can I ask why you're not just retrieving the data without concatenating it all together?
SELECT body,
IF(screen_name IS NOT NULL AND !anonymous, screen_name, '') AS replier,
created
FROM idiscussion
LEFT JOIN
users
ON idiscussion.uid = users.id
WHERE idiscussion.iask_id = 1
This should execute as quickly as your original query, and you can access its data as you would any other result set. Assuming you're using MySQL and not using PDO:
$result = mysql_query($sql);
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
// $row is an associative array containing the current row's data.
}
}
If you need to create the XML but also want to access the data in its raw form, just create it in the PHP.
$xml =
'<comment>' .
'<body><![CDATA[' . $row['body'] . ']]></body>' .
'<replier>' . $row['replier'] . '</replier>' .
'<created>' . $row['created'] . '</created>' .
'</comment>';
You may need to escape the data if it hasn't been sanitised already. You could also use an XML builder such as XMLWriter, though that's beyond the scope of this question.

Categories