Retrieve serialize value in the database wordpress - php

I'm trying to query a serialized array value in the database in wordpress, value will be stored in the table wp_postmeta, in the column meta_value.
Well, first I stored the array by using serialize() function of php.
So for example,
$postID = 1;
$arr = array(1, 2, 3);
$ser_val = serialize($arr);
update_meta_data($postID, '_customvalue', $ser_val);
The stored values is something like this
s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}";
Then when I tried to retrieve it by performing wordpress sql query.. What I was expecting that it will be an array since it is stored as array, but after doing so, it display as string not an array.
$get_score = $wpdb->get_row("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_cummulativescore'");
$scr = unserialize($get_score->meta_value);
var_dump($scr);
//output displayed
//string(30) "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
I did check the value using is_array() function, the result is that it is not an array
Any idea on this to get the serialize value as an array?

It looks like your data was converted to a string during serialization.
The data should be stored as
a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}
instead of
s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
s:30 means string length 30.

Related

Save data in json format using php

I wanted to save the data in this format into database option field:
{"type":"type_value"}
I am getting data in post varaibles like this
$type_value = $request->input('type_value');
$type = $request->input('type');
How Can I save this in database?
I have tried this
$data['options'] = array($type,$type_value);
But by this it is saving in this format:
["Qualifiers","1"]
I even tried doing this:
$data['options'] = json_encode(array($type,$type_value));
Instead it is saving like this
"[\"Qualifiers\",\"1\"]"
how can I do this?
You just have to change your array definition. Your array considers 2 different elements i.e type and type_value. So just make your array with key value pair and you are all set.
json_encode(array($type => $type_value))
Check this :- Fiddle

how to store array values in single field database using json_encode in luman?

how to store array values in the single field database using json_encode in luman?
I get value from the request:
$qualification_id = array($request->input('qualification_id'));
my json encode line:
$serializedArr=json_encode( $qualification_id);
my insert query:
$result = DB::insert("insert into `borrower_registration`
(first_name,middle_name,last_name,city_id,
state_id,dob,marital_id,father_husband,
institute,qualification_id,graduated_id)
values ('$first_name',' $middle_name','$last_name','$city_id',
'$state_id ','$dob ',' $marital_id','$father_husband',
'$institute','$serializedArr','$graduated_id')");
I given sample input for the array 1,2,3,4 and using
print_r($qualification_id)
and get output like [1,2,3,4]
data type of the column qualification_id is int
I execute the code and it store in database as 0 .please give valuable suggestions.
Check if qualification_id column has JSON or TEXT format.

How to assign array to array variable which is store in a database

I store array structure in database table.
example
table name - example
id=1
data= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
I want to get that array structure from table and assign data column to an array.
Example
while($row=mysqli_fetch_array($result))
{
$table=$row['data'];
}
I did this way.. but it's not working.
It results in:
$table[0]=>array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
You can save array data to database field in multiple ways.
I suggest two ways:
1) Serialized array:
you can save data using serialize() function.
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = addslashes(serialize($arr));
And then you can unserialize() them to get it back like:
$toDb = unserialize(stripslashes($fromDb));
2) Using json_encode() and json_decode();
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = json_encode($arr);
And then you can json_decode() them to get it back like:
$toDb = json_decode($fromDb)

Serialized multidimensional stored in MySQLi does not print past first array

Confusing title, the basics are that I'm saving a fully sorted and ordered multidimensional array from a script and into MySQL. I then, on another page, pull it from the database and unserialize it, and then proceed to print it out with this,
$s = "SELECT * FROM gator_historical_data WHERE channelid = '{$chanid}'";
$r = $link->query($s);
$comboarray = array();
while ($row = mysqli_fetch_assoc($r)) {
$comboarray[] = unserialize($row['dataarray']);
}
foreach ($comboarray as $item) {
$desc = $item['content']['description'];
$title = $item['content']['title'];
$datetime = $item['datetime'];
// ... ^^^ problems getting array data
}
The problem is that it doesn't take the full array from MySQL, only the first entry and thus only prints the first 'array'. So where the returned value from dataarray looks like this (var_dump): http://pastebin.com/raw.php?i=Z0jy55sM the data stored into the unserialized $comboarray only looks like this (var_dump): http://pastebin.com/raw.php?i=Ycwwa924
TL;DR: Pulling a serialized multidimensional array from a database, unserializing and it loses all arrays after the first one.
Any ideas what to do?
The string you've got is a serialized string plus something more at the end that is also a serialized string again and again:
a:3:{s:6:"source";s:25:"World news | The Guardian";s:8:"datetime ...
... story01.htm";}}a:3:{s:6:"source";s:16:"BBC News - World";
^^^
This format is not supported by PHP unserialize, it will only unserialize the first chunk and drop everything at the end.
Instead create one array, serialize it and store that result into the database.
Alternatively you can try to recover for the moment by un-chunking the string, however in case the paste was done right, there are more issues. But on the other hand the paste obvious isn't the done fully correct.

How to store MySql values as Multidimensional Array using PHP

I have a database table as follows.
<table border='1'><th>Id</th><th>FirstName</th><th>last Name</th><tr><td>1</td><td>Tom</td><td>T</td></tr><tr><td>2</td><td>Jerry</td><td>J</td></tr></table>
I would like to store all values as a multi dimensional array using php(using a while loop to retrieve fields).That is,
I would like the data to be echoed as:
array(array(1,Tom,T),array(2,Jerry,J));
$result = mysql_query("SELECT * FROM tablename;");
while($result_ar = mysql_fetch_array($result)) {
$multid_array[] = $result_ar;
}
after which $multid_array will be an array of arrays.
You can use phps serialize function to convert any variable into a string representation
$string = serialize($dbData);
You can use unserialize() to convert the string back into arrays, objects, etc
$dbData = unserialize($string);
Once you have the data within a string, it's very easy to store in a file, db, etc. A disadvantage is that you won't be able to search your database easily for this data.

Categories