I have a problem, to obtain the values of the table wp_postmeta and meta_key "construccion_value", the results are serialized. This is the code I use.
$datos = get_post_custom_values('construccion_value');
$datos= maybe_unserialize($datos);
print_r($datos);
When do I print these results appear
Array ( [0] => a:2:{i:0;s:0:"aaa";i:1;s:3:"sss";} )
My question is how do I get the string "aaa" and "sss" respectively.
Now try it with maybe_unserialized and unserialized, but without results on screen.
I don't know what maybe_unserialize is, but unserialize will work.
Try =
$datos = unserialize($datos[0]);
Related
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.
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.
I am trying to select a variable from an array (at least I think it's stored as an array):
$data = json_encode($response);
file_put_contents('file.txt', $data);
gives
"status":200,"response":{
"api_id":"0f42d6be-8ed2-11e3-822e-22135",
"bill_duration":36,
"call_duration":36,
"total_rate":"0.00300"}
}
How can I select the call_duration value (in php)? I've tried $response['call_duration'], which I thought should work but returns nothing?
$response['call_duration'] was very nearly correct, but I think you need:
$response['response']['call_duration']
Looking at your output after converting to json, I think the original array, $response, looks like this (in PHP array format)
$response = array(
'status'=>200,
'response'=>array(
'api_id'=>'0f....etc',
'bill_duration'=>36,
... etc
)
);
So, you need to go an extra level deep into the array to get call_duration.
I have a "recruiter" table in my database which has different attributes and one of them is "Professions". "Professions" is a serialized array which I get from a multiple select form. And this works fine.
When I unserialize this attribute nothing is printed - no error, no text.
This is a code I was testing serialization with:
$sql = 'SELECT Company_name, Status, Size, Professions, Seniority_levels, Sector, Website, Location FROM Recruiter';
$query = mysql_query($sql, $con);
while($result = mysql_fetch_array($query, MYSQL_BOTH)){
$recruiters[] = array($result[0], $result[1], $result[2], $result[3], $result[4], $result[5], $result[6], $result[7]);
}
foreach($recruiters AS $recruiter){
$test = unserialize($recruiter[3]);
echo $test[0].'<br>';
}
So basically $test[0] prints nothing although the new lines are printed. Please help!
try printing the $test array and the $recruiters and the $recruiter arrays. See if the result is fine before the unserialisation of the data. If the query returns any data. Also try the while loop with mysql_fetch_assoc. Let me know of the results and if this solves the problem
test = unserialize($recruiter[3]); should become test = unserialize($recruiter[5]); since the sector field is the sixth column .
However what if somewhere in the future you might need to select rows where sectors equal smth ? serialize whont help you then so i suggest you have a look at a different implementation for the sector filed witch is called bitwize http://www.litfuel.net/tutorials/bitwise.htm
Edit
Asuming you hit the right column and the column contains a:1:{i:0;s:27: a:1:{i:0;s:27: a:38:{i:0;s:27: a:9:{i:0;s:39:, it looks like the serialized array is not fully saved in you're db, it's only part of it . So the unserialize function whont return you an array . Have a look at the length of the mysql field i assume you've set it smaller than you need so you're data is trimmed on insert/update .
Edit
a:1:{i:0;s:27: you're still missing the rest of the serialized array . s:27: means a string is following containint 27 characters, and you're serialized array stops there when it should look like
a:1:{i:0;s:27:"123456789012345678901234567";}
( a:1 stands for an array containing 1 value with it's content between {}, i:0; is the array key 0, s:27:""; stands for a string containing 27 characters as the value for the i:0 key ) .
I have a multidimensional array in PHP like this:
$array = array(
"Part1" => array(
"Subpart1" => array(0, 1),
"Subpart2" => array(1, 0)
),
"Part2" => array(0),
"Part3" => array(0, 1, 0)
);
Now I want to store this array in a MySQL table and retrieve it exactly like this again on another PHP page.
I've been trying using serialize() and unserialize()
$array= serialize($array);
and then on the other page
$array= $row['Array'];
$array2 = array();
$array2 = unserialize($array);
But I seem to do something wrong, in the beginning I got a var_dump of bool(false) and now I get a var_dump of NULL.
Your code looks ok...
One thing that can catch you out is if your column is too small - if you use VARCHAR(255) your data can be truncated and won't unserialize. If you add the value of $row['Array'] I could see if it's whole.
Use column type TEXT. Serialized data often doesn't fit VARCHAR(255).
You could use json encode, json_encode($array) and you will get a string value in json notation so you can store in database, and retrive and do a json_decode($string, true) so you cand convert in an array again. If you don't pass the true argument to the json_decode, it will converted to a stdClass.
Particulars
Quantity
Rate
Amount
Amount Sum
Others
Others Amount
Tax
Total
This is my table in php/html.
I need to store and retrieve the php Particulars(p),Quantity(q),Rate(r) as a separate table format