I have this following.
id name
1 Dhiraj Dhanaji Desai.
2 ram patil
3 aman mehta
I want this.
id name
1 Dhiraj Dhanaji Desai.
ram patil
aman mehta
You can store JSON value in a field for having "multiple" data in a single field.
for example, you can store this value:
{"id":1,"name":"Dhiraj Dhanaji Desai"}
and get it back parsing JSON. (for example php have a function json_decode)
references:
https://www.php.net/manual/en/function.json-decode.php
https://www.json.org
Related
I have a php form that when submitted sends form values into a MySQL Database named "Hotel" in a table named "Reservations" that has one column titled "Form". In the "Form" column, each form field is enclosed within {} and fields are separated by commas. Here is what the data looks like in the "Form" column:
[{"id":"1","translation":"Token","value":"123456789"},
{"id":"2","translation":"Name","value":"John Smith"}]
Desired MySQL Query Result: I want to grab each "translation" and "value" in my query and have them put into separate columns. Column 1 title "Token", Column 2 title "Name" then list the values below each
--------------------------
| Token | Name |
--------------------------
| 123456789 | John Smith |
--------------------------
I have never run into this kind of data in a column before so I am unsure how to create the query. I'm thinking substring perhaps? Any help or guidance is greatly appreciated. Please let me know if more information is need from me to help process the request.
The form column has data format in json.
Simply fetch the column value and use php function
$result = json_decode(data);
Now $result holds the data in array format.
Use for each to iterate the array and fetch each value.
First, let's take the JSON string that's in the Form column and turn it into an array with json_decode(). Assuming you've already retrieved the value from the Form column and assigned it to the variable $form:
$form = json_decode($form,true);
Next, we'll retrieve the Token value and the Name value:
$token = $form[0]["value"];
$name = $form[1]["value"];
Note: This assumes that 'token' always occurs first in the 'form' string, and that 'name' always occurs second.
You can it do like this:
Samples to get the Values:
SELECT REGEXP_REPLACE('[{"id":"1","translation":"Token","value":"123456789"},{"id":"2","translation":"Name","value":"John Smith"}]',
'^.*"translation":"Token","value\":"([0-9]+)".*$','\\1') AS Token;
RESULT: 123456789
SELECT REGEXP_REPLACE('[{"id":"1","translation":"Token","value":"123456789"},{"id":"2","translation":"Name","value":"John Smith"}]',
'^.*"translation":"Name","value\":"(.*)".*$','\\1') AS Name;
RESULT: John Smith
To Update the Table:
update TABLENAME set
TOKENFIELD = REGEXP_REPLACE(JSONFIELD,{"id":"2","translation":"Name","value":"John Smith"}]',
'^.*"translation":"Token","value\":"([0-9]+)".*$','\\1'),
NAMEFIELD = SELECT REGEXP_REPLACE(JSONFIELD,{"id":"2","translation":"Name","value":"John Smith"}]',
'^.*"translation":"Name","value\":"(.*)".*$','\\1');
I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html
i want to store my PHP values in db(mysql) in the form of array...
for example
$a=array{10,20,30,40};
i want to store this variable $a in to db in the array form like how it's storing in array using index.
why i want to do this because in future i may have to perform update or delete operation on the array values..
i know that it's possible to do this thing... but i don't know how to implement this..
i searched about this topic but i didn't get proper answer....
Please suggest me how to do this things...
Why don't use json_encode in PHP and store it on your database. It's the best way.
The array will be converted to a string and will be stored.
Retrieve the data and make use of json_decode and then start working as per your needs.
Example:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
OUTPUT: {"a":1,"b":2,"c":3,"d":4,"e":5}
You should create a distinct TABLE to store this kind of data.
Consists of 2 columns, corresponding record ID and the actual data.
So, your record will be looks like
rid value
1 10
1 20
1 30
1 40
2 10
2 40
...
this way you will be able to perform update or delete operation on the array values using conventional SQL routines, as well as selecting data based on the array values.
This is how the things done oin the real world, not in PHP sandbox.
All othe answers here are plainly wrong
I would use serialize/unserialize for this. You can use it like this:
Send to MySQL
<?php
$a = array{10,20,30,40};
$a = serialize($a);
// your code here to send it to the mysql
?>
Get from MySQL
<?php
// your code here to collect it from mysql
$a = unserialize($mysql->str);
?>
The field in the MySQL should be TEXT or VARCHAR.
Regards
BlackBonjour
You can always serialize your array then store the result in a VARCHAR or TEXT field and after fetching you can unserialize the field.
I have a table, named "stu_id" which contain 3 fields, like "sem_id", "crs_id", "session" with some of data.
"sem_id"(cse1/1, cse1/1, cse1/1, cse1/1, cse1/1)
"crs_id"(cse101, cse102, cse103, cse104, cse105)
"session"(spring2009, spring2009, spring2010, spring2009, spring2010)
![stu_id][table]
Now, i want to count how much type of session is there in session field (Of course here the answer will be two types). Its not like the number of "spring2009" and "spring2010" data in that field. Simply want to count the "type of session". So, what will be the mysql query for this.- Thank you.
SELECT count(sess) from (SELECT distinct(session) as sess FROM stu_id);
If that's what u want... :)
My wp_usermeta table has 4 columns, umeta_id | user_id | meta_key | meta_value
Image of table:
One of the columns has serialized data - wp_s2member_custom_fields. How can I unserialize from mysql or output with php to see all of my users data within the serialized column?
Here is a breakdown of the serialized data:
wp_s2member_custom_fields
a:12:{
s:7:"country";
s:2:"CA";
s:4:"city";
s:8:"Brampton";
s:5:"state";
s:7:"Ontario";
s:8:"zip_code";
s:6:"L6T4E7";
s:3:"age";
s:13:"25–34 years";
s:8:"blog_url";
s:22:"http://www.blog.com";
s:16:"blog_description";
s:106:"A blog about blogging";
s:15:"monthly_uniques";
s:4:"1000";
s:13:"facebook_page";
s:55:"http://www.facebook.com/myfacebookpage";
s:14:"facebook_likes";
s:3:"1428";
s:15:"twitter_account";
s:31:"http://twitter.com/mytwitterpage";
s:17:"twitter_followers";
s:3:"5849";}
Fetch the data from the database and use PHP's unserialize(). There's no way of doing this in MySQL (or any other DB) and it's the main reason that most developers prefer to just write comma-separated valies in their tables.
WordPress stores arrays/objects in the database by serializing them, so usually the retrieval function is what you want to look for since it will return the data unserialized.
In this case, it's user_metadata, so you need to use the function get_user_meta(), which you provide the $user_id and $key parameters to retrieve it.
In your example, it would be something like this:
<?php
$array = get_user_meta(760, 'wp_s2member_custom_fields');
var_dump($array);
I hope that helps and makes sense...
NOTE:
The WordPress Codex is a priceless resource!