I am using PostgreSQL on a Laravel installation. A table has a bytea type field which is being used to store binary data (base64_encoded file contents).
When I use Eloquent to retrieve the table I get a resource type variable being returned in this field.
How can I rather retrieve this as a string?
$raw = Media::where('id','=',$id)->first();
$raw->file_data = base64_decode($raw->file_data); // doesn't work
As the author of this question did not post the details to the answer, I will post my findings here.
As the returned field is a handle to a stream you can use the stream_get_contents function to read the value into a string, you can then use pg_unescape_bytea to get the actual value of the bytea data. Finally use the htmlspecialchars function if you wish to display the bytea data in HTML.
Example code:
$my_bytea = stream_get_contents($resource);
$my_string = pg_unescape_bytea($my_bytea);
$html_data = htmlspecialchars($my_string);
The answer was to use stream_get_contents on the stream. duh.
Related
How to correctly save HTML content in the MYSQL table using PHP. I used the following functions of PHP but no luck.
$encodedHTML = htmlspecialchars($cs_htmlEditor);
$encodedHTML = urlencode($cs_htmlEditor);
You can try base64_encode and base64_decode, your column data type should be BLOB.
To encode data in base64
$encodedHTML = base64_encode($cs_htmlEditor);
To retrieve the data
$decodedHTML = base64_decode($cs_htmlEditor)
I have the following problem. I am retrieving a mysql text field that is a serialized text of a invoice. I am working in 2 different projects. Both have the same version of PHP. The data was exported & imported from db to db. If i var_dump the data from db1 it tells me it's length is x. When I do the same in from db2 i get x+2
string(595)
"a:3:{s:11:"userdetails";a:20:{s:4:"name";s:3:"bas";s:8:"lastname";s:7:"schmitz";s:5:"email";s:17:"email#test.de";s:6:"street";s:11:"f�rstenwall";s:7:"street2";s:0:"";s:7:"company";s:0:"";s:3:"zip";s:5:"40215";s:9:"residence";s:10:"d�sseldorf";s:7:"country";s:7:"Germany";s:5:"phone";s:7:"3033185";s:3:"fax";s:0:"";s:10:"customerID";i:202771;s:2:"nr";s:3:"228";s:6:"region";s:3:"nrw";s:10:"phone_code";s:3:"211";s:8:"fax_code";s:0:"";s:10:"salutation";s:2:"Mr";s:5:"sales";s:0:"";s:12:"country_code";s:0:"";s:10:"vat_number";s:0:"";}s:6:"domain";s:15:"bas-schmitz2.de";s:10:"has_domain";b:1;}"
string(597)
"a:3:{s:11:"userdetails";a:20:{s:4:"name";s:3:"bas";s:8:"lastname";s:7:"schmitz";s:5:"email";s:17:"email#test.de";s:6:"street";s:11:"fürstenwall";s:7:"street2";s:0:"";s:7:"company";s:0:"";s:3:"zip";s:5:"40215";s:9:"residence";s:10:"düsseldorf";s:7:"country";s:7:"Germany";s:5:"phone";s:7:"3033185";s:3:"fax";s:0:"";s:10:"customerID";i:202771;s:2:"nr";s:3:"228";s:6:"region";s:3:"nrw";s:10:"phone_code";s:3:"211";s:8:"fax_code";s:0:"";s:10:"salutation";s:2:"Mr";s:5:"sales";s:0:"";s:12:"country_code";s:0:"";s:10:"vat_number";s:0:"";}s:6:"domain";s:15:"bas-schmitz2.de";s:10:"has_domain";b:1;}"
As I am pasting these I can see that there is a difference when displaying germanic characters
Any idea to why this is happening?
The output of serialize() cannot be handled as plain text:
Return Values
Returns a string containing a byte-stream representation of value that
can be stored anywhere.
Note that this is a binary string which may include null bytes, and
needs to be stored and handled as such. For example, serialize()
output should generally be stored in a BLOB field in a database,
rather than a CHAR or TEXT field.
Thus your data is corrupted in the first place.
If you're unable to change the database design (which would be the proper fix), you need to re-encode serialised data in a plain text encoding such as Base64:
$encoded = base64_encode(serialize($foo));
$decoded = unserialize(base64_decode($encoded));
I have a string I want to save into my DB in an encrypted format. I am using the security utility provided by cake so this is the code I use to encrypt my sensitive data:
// get my encryption key
$encrypt_key = Configure::read('Secret.encrypt_key');
// encrpyt this string to be stored in the database
$this->request->data['User']['message'] = Security::rijndael($this->request->data['User']['message'], $encrypt_key, 'encrypt');
// save this user data
$user_saved = $this->User->save( $this->request->data[ 'User' ] );
This looks like ever guide I have seen for how to do this, but in my case all of the other fields will save and I will get an empty field for message
My question is why is this blank database save happening and how do I fix it. Thank you.
The problem ended up being that the rjindeal function returns a raw binary string 010100100010100101111101010010101 that my database cannot handle. By simply converting the result to hex code via bin2hex($encrypted_message) the data is transformed into a form that my database can handle.
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);
Actually I had get the image string from the JSON in my android project. The string image successfully pass to using HTTPPOST.
Now, I face the problem on saving an image as blob type in mysql database in php.
How to convert the string image i get so that it can be save as a blob type in mysql database?
If anyone know the way please kindly advice.
Thank you.
Just base64_decode() the data:
$blob = base64_decode($json_64_encoded_string);
Then write it to the DB. If you need help getting the encoded string out of the POST array have a look at the PHP json_decode function: http://php.net/manual/en/function.json-decode.php
$json_obj = json_decode($_POST[post_key]); //replace 'post_key' with whatever you use
$blob = base64_decode($json_obj->blob); //replace 'blob' with whatever you use