Saving utf-8 array in database laravel - php

I have problem with saving utf-8 array in database with laravel.
when i save array in database it is stored like this
\u10e4\u10dd\u10dd\u10d3
when i display data it works fine, but when i run search query it displays nothing.
database encoding is utf-8 with general_ci collation.
i've already tried to encode data before saving in database
json_encode($data, JSON_UNESCAPED_UNICODE);
any ideas?

Try storing these arrays in your database using the PHP serialize function instead of encoding for JSON.
serialize($data);
When you want to use the data back, simply deserialize the array with PHP unserialize function.
unserialize($data);

Related

PHP: Converting this data structure into an associative array

I have a field created by word press which contains data with the following structure.
a:16:{s:7:"country"
s:14:"United Kingdom"
s:7:"form_id"
s:2:"35"
s:9:"timestamp"
s:10:"1560869327"
s:7:"request"
s:0:""
s:8:"_wpnonce"
s:10:"125"
s:16:"_wp_http_referer"
s:1:"/"
s:17:"ajaxy-umajax-mode"
s:8:"register"
s:10:"first_name"
s:5:"xxxxx"
s:9:"last_name"
s:5:"xxx"
s:10:"user_email"
s:28:"xxx#xxx.co.uk"
s:7:"Company"
s:16:"xxx LTD"
s:12:"phone_number"
s:10:"0123456789"
s:8:"user_url"
s:20:"http://www.test.com"
s:15:"company_address"
s:18:"999 LockSmith Lane"
s:12:"display_name"
s:12:"XXXX"
s:10:"user_login"
s:10:"xxx123"
}
I want to convert this to an array so I can read the properties of it.
I tried converting it to json but its not json.
Any ideas on how I can parse this data, or access its properties in PHP.
I can not access this data through wordpress as my PHP script is part of something else.
Seems like a serialized array. Try unserializing it to convert it back to normal, see example.
This is Actually not a json.This is an array in serialized format you can just unserilize it by using this function
maybe_unserialize($YOUR_ARRAY).
maybe_unserialize is a wordpress default function to unserialize the array

How do I decode data in mysql field that is in this format?

I have fields in my database that I need to decode or view in a more simple format. I'm not sure what method was used to create this format though I've seen it before. This is data from a web form in a MySQL table. What do I use in PHP or MySQL to decode this when I retrieve it from the database?
a:10:{s:10:"First Name";s:10:"cvsgjsrhlw";s:9:"Last Name";s:10:"cvsgjsrhlw";s:7:"Address";s:26:"http://www.tlneepxlni.com/";s:4:"City";s:7:"Atlanta";s:5:"State";s:2:"AL";s:8:"Zip Code";s:0:"";s:9:"Best time";s:7:"Mid-day";s:6:"Other2";s:8:"cqoeqipd";s:14:"Procedure face";s:18:"Laser Hair Removal";s:4:"when";s:22:"In the next 4 months.";}
It's a built in php serialization
You need to use unserialize
This is serialize()'d code. It is a way to multiple data types as plain text. You can convert it back to a php object with unserialize($data)
If you are writing new code, I would recommend using json_encode() instead
This is a serialized array. Use:
$data_array = unserialize($data_from_db);

how to get php session array to javascript array

I have a problem to get in a session array from php into a javascript, I tried to do it with json_encode, but I got problem with that since json not take swedish charatar and I need to have it in the array, any other suggestion how to do it!
I´m setting the sessions array here, the print_r is only for checking so it´s correct
while($row2[]=mysql_fetch_array($result2))
$_SESSION['row2'] = $row2;
print_r($_SESSION['row2'][0][0]);
print_r($_SESSION['row2'][0][1]);
print_r($_SESSION['row2'][0][2]);
and in my javascript i tried this
var row2 =<?php echo json_encode($_SESSION['row2']) ?>;
console.debug(row2);
Suggestion: Tell your database-client that you expect the data to be encoded as UTF-8 when you request/fetch it from the database. You do this by setting the client character encoding, see mysql_set_charset­Docs.
When you retrieve the data encoded as UTF-8 you can use it with json_encode and your website script's can properly receive it.

json encode in php

I am storing this {background:"default.jpg"} in the database in field of tables as i am taking the table fields data and makint the json by json_encode while encoding this json also get encoded and it is not valid json so how should i encode these json.
{"id_session":"c72b0581e7675b596a7651a7bb906438","gibid":"54","name":"Market Place","type":"S","num_owners":"0","inner_template":"","inner_data":"{background:\"default.jpg\"}","outer_template":"","o
it is adding the slashes how should i get return the valid json.
thanks.
Don't store JSON in the database, store it in a neutral format, like key/value columns. Or:
Decode the JSON, merge it into the array to be encoded, then encode it.
$data = array('id' => ...);
$data['inner_data'] = json_decode($databaseJson, true);
json_encode($data);
So you have a JSON string in the database and then you get it out of it and do a json_ecnode on ir again? For php you are just encoding some string (thats why the backslashes on the quotations marks are coming from).
But to achieve what you actually want, you could to decode the JSON string from the database first and then encode it with the rest of your data again.

dump xml string verbatim to mysql db

I need to dump an xml string (coming into a web service as a POST param), directly into a Mysql DB. The column into which I am writing is currently of type 'text', though I have tried blob as well.
Right now, when I save the string and retrieve it later on using sql, it comes back in a serialized like format, like this:
a:1:{s:14:"<?xml_encoding";s:1502:"UTF-8?><record>
<nodes></nodes>
</record>";}
Whereas, I need to parse that xml as a simplexml object, and hence need it to be better formed.
This is what I am doing codewise:
Accept Post Param in web service
Serialize and store it in DB using doctrine ORM, kind of like
$record->xml_rec = serialize($_POST)
Retrieve it and echo it.
Oddly enough, if I unserialize and echo is upon retrial, I get an array. The array looks like this upon print_f
Array
(
[<?xml_encoding] => UTF-8?><record>
<nodes></nodes>
</record>
)
Still not parse-able by simplexml.
Is this a mysql storage problem or am I doing something wrong with the POST params? I've tried htmlspecialchars to save the string in the db, but similar problems remain.
any help is appreciated, thanks
What you have is a "serialized" php array that you can unserialize by using ... PHP's unserialize function.

Categories