return group_concat data as array - php

I want to return values I retrieve from the db using group_concat as an array of data. Is it possible to do this in the mysql query? Or do I need to explode the data into an array?
GROUP_CONCAT(sh.hold_id) as holds
returns this
[holds] => 3,4
I want it to return:
[holds] => array(3,4)

As I said in my comment: you need to explode the data into an array, using php code like this:
$holds = explode(',', $holds);
because mysql has no concept of array-type for data.

This is possible since MySQL 5.7.22 using the JSON_ARRAYAGG() method
Read more: https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_json-arrayagg
Example:
SELECT JSON_ARRAYAGG(category.slug) as categories From categories

If you need to do it on the MySQL level, and then you may probably parse it to an object.
You can do the following
SELECT CONCAT("[", GROUP_CONCAT(category.name), "]") AS categories
From categories

MySQL has no concept of arrays. Therefore it is not able to return an array. It is up to your processing code (here the php scripts) to convert the concatenated notation into a php array.

It is possible to return mysql JSON array like so,
json_array(GROUP_CONCAT(sh.hold_id)) as holds
Refer docs for further info.

Related

Codeigniter accessing array values

I simply want to know how to access array elements retrieved from a database. I have the following code to get the names of each item in my database.
$plat_options = $this->db->get('tblplatform_options')->select('name')->result();
How do I go about accessing the name from the array $plat_options? Typically I would do $plat_options[0] for the first element in C#, how is this done in php/codeigniter?
In PHP/Codeigniter, can be done in the same way:
$plat_options[0] //if you have this element, usually is better to check if exists.
You can retrieve all the elements with foreach($plat_options as $option){...}
You can cast to object: https://www.kathirvel.com/php-convert-or-cast-array-to-object-object-to-array/
Or use a Codeigniter Helper (assuming you are using CI3): http://www.codeigniter.com/user_guide/helpers/array_helper.html
I recomend to know which is your array format and retrieve that way (if you don't know, you can do a: var_dump($plat_options) ) to know if is an associative array.
You can use the result_array() function:
$data = $plat_options->result_array();
echo($data[0]['name']);
or:
$data = array_shift($q->result_array());
echo($data['name']);
I extracted this last part from: Codeigniter $this->db->get(), how do I return values for a specific row? that you could check too.
If you don't know a lof of CI, the best you can do is do a simple tutorial to understand how the data + ActiveRecord works.
Hope it helps!

Either PHP convert JSON values to have keys or PostgreSQL assign values to keys

I simply need to store text (rows have their own separate date).
Either...
1. How do I read (either of) the following value(s) in to a PHP array that I can iterate over via a foreach loop?
...or...
2. how do I INSERT INTO a table a value assigned to a key (which is not clarified at all in the PostgreSQL documentation)?
{".022 x 2.031"}
{".012 x 3.621"}
So I currently have single measurement values (to be treated as text purely for records) formatted as the first format above. I try to convert the row data in PHP and get Invalid argument supplied for foreach() for the following:
$array = json_decode($row1['material_size']);
foreach ($array as $k1) {echo $k1;}
So after doing even more searching and reading I decided to see if I could just convert the data via PostgreSQL directly:
SELECT array_to_json(material_size) AS material_size FROM table;
Now I get the array data correctly...
$array = json_decode($row1['material_size']);
print_r($array);
Array
(
[0] => .022 x 2.031
)
Afaik, dimension is a structured data. If your data is 2 dimensional, you may use the point datatype or create your own composite type. I'd suggest to use Pomm to get it converted to a PHP array.
If you want to stick with Json then you can select on keys like stated in the documentation but you need to overwrite the complete JSON value every time you want to update a value.

PHP query parsing couchdb

I am new to php so, need any help for this issue
I tried to run query
localhost/ls-test/_design/app-views/_view/timestamp-measurement?descending=true&include_docs=true
and get json return something like this format:
{"total_rows":441740,"offset":0,"rows":[
{"id":"123","key":[2013,2],"value":["1","PowerConsumption","0.0"],"doc":{"_id":"5446691910660799","SourceType":"SENSOR","SourceId":"4294403072","Property":"PowerConsumption","value":"0.0","}},
{"id":"124","key":[2013,2],"value":["228224","Motion","false"],"doc":{"_id":"5446688472521031","SourceType":"SENSOR","SourceId":"228224","Property":"Motion","value":"false","timestamp":"2013-02-12 17:06:06.768"}}
...]}
so, how do I further query it in PHP that my result only displayed based on specific value e.g only list of documents with SourceId:228224
You would json_decode in PHP then work with your associative array.
http://php.net/manual/en/function.json-decode.php

build SQL statement from json encoded string php

Welcome Stackoverflow nation ! I'm trying to decode json encoded string into SQL statement withing php.
Lets say I've such a json encoded string =>
$j = '{"groupOp":"AND","rules":[{"field":"id","op":"cn","data":"A"},{"field":"i_name","op":"cn","data":"B"}]}';
I want to build SQL WHERE clause (needed for filterToolbar search in jqGrid), something like this => "WHERE id LIKE %A% AND i_name LIKE %B% " and etc.
I've done this =>
$d = json_decode($j);
$filterArray = get_object_vars($d); // makes array
foreach($filterArray as $m_arr_name => $m_arr_key){
// here I can't made up my mind how to continue build SQL statement which I've mentioned above
}
Any ideas how to do that , thanks preliminarily :)
The first problem is you will want to pull out the groupOp operator.
Then, you have an object and inside that you have an array of objects, so you may want to look at the results of filterArray as that won't have the value you want.
Then, when you loop through, you will want to do it with an index so you can just pull the values out in order.
You may want to look at this question to see how you can get data out of the array:
json decode in php
And here is another question that may be helpful for you:
How to decode a JSON String with several objects in PHP?
There is an answer with an implementation for the server-side php code here
Correction: I had to unescape double-quotes in the 'filters' parameter to get it working:
$filters = str_replace('\"','"' ,$_POST['filters']);

How to Work with PostgreSQL Function Output in PHP

I have a postgresql (V 8.4) function that returns SETOF a custom type. An example of the string output from this function is the following 4 rows:
(2,"CD,100"," ","2010-09-08 14:07:59",New,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(5,CD101,asdf,"2010-08-08 14:12:00",Suspended-Screen,1,10000,,,,,,,)
(4,DNR100,asdf,"2010-09-08 14:10:31",Suspended-Investgate,0,,,,,,"2010-09-06 16:51:51","2010-09-07 16:51:57",)
(3,MNSCU100," ","2010-09-08 14:09:07",Active,0,,,,,,,,)
I need to work with this data in PHP and I'm trying to figure out the best way to work with it. What I would love is if there was a way for postgresql to return this like a table where columns represent each value within a record rather than as a comma-separated string.
Is this possible? If not, what is the best way to work with this comma-separated string of values in PHP?
I've see this post (Convert PostgreSQL array to PHP array) and can use the function mentioned there but I wanted to ask if anyone has other ideas or suggestions.
Thanks,
Bart
There's str_getcsv() which'll parse a string as CSV data and return an array of the individual fields
Yep, its real easy, just change the way you are calling the function.
Instead of
SELECT my_srf(parm1);
Do either:
SELECT * FROM my_srf(parm1);
SELECT (my_srf(parm1)).*;
You'll even get the column names out this way.

Categories