I am saving data in a mysql database. This data is an array and the content is different data of the current user that is logged in to my system.
I do this when I save to the database:
$data = addslashes(serialize($array));
then
"UPDATE or INSERT INTO TABLE SET ... data = '$data';"
Now, the data are saved correctly since the insert or update statement return valid from my php code.
My problem is when I try to un-serialize it, it returns false and a notice is shown in my page.
What am I doing wrong?
I will bet the field in your mysql database is not big enough to save all the characters. That is why, when you un-serialize it you get an notice and nothing in return.
Try to increase the field to a MEDIUMBLOB or MEDIUMTEXT (maximum length of 16,777,215) or LONGBLOB or LONGTEXT (maximum length of 4,294,967,295) like this:
ALTER TABLE your_table MODIFY COLUMN column_name MEDIUMTEXT /* other properties*/;
And try to save and read your data again.
Now, if your data is over 4,294,967,295 (which is LONGBLOB or LONGTEXT), maybe you should check what kind of data you saving and maybe filter or remove unwanted some.
After you're getting the data from the table, are you removing the slashes before unserialize function.
Try inserting without the addslashes() and add slashes before it's taken to the array.
Related
I want to go through the database one row at a time. The aim is to go through all the entries in a database table to update a column.
simplified DB table
id
data
ratings
1
jsonstring*
ratingstring*
Note: There are over 200,000 entries.
(*)jsonstring
{"ID":78,"post_author":1,"active":1,"a_rating_users":71,"a_rating_score":278,"a_rating_avarage":"3.92","t_rating_users":6,"t_rating_score":19,"t_rating_avarage":"3.17","r_rating_users":5,"r_rating_score":22,"r_rating_avarage":"4.40"}
(*)ratingstring
{"d":{"r":"1","c":1,"a":1},"t":{"r":5,"c":1,"a":5},"p":{"r":5,"c":1,"a":5}}
data are CHARACTER SET: utf8 and TYPE: TEXT
ratings are CHARACTER SET: utf8 and TYPE: Varchar
My current approach:
I get all the data and iterate it with a foreach loop. Since it still has to
decode the string into a JSON for each entry, it takes a long time.
How can I optimise it? One consideration was to read each row individually from
the database, modify it and save it. But I don't know if this would work and how
to move the database pointer to the next entry.
I am currently storing JSON data in a gzipped LONGBLOB column. I want to change this column to a JSON column so that O can use the JSON functions in MySQL.
However, when I tried to insert a row using MySQL with a JSON column, I realised that the insert_id was always 0 and all the inserts failed. I then checked the PHP MySQLi documentation and realised that send_long_data (which I was using to send the JSON data) is only usable for TEXT and BLOB columns.
Is there an alternative to send_long_data which works for JSON columns? Sending the JSON data using bind_param and a string would not work as I believe most of my data would exceed max_allowed_packet.
You can do this, but it's bad practice:
Create a TEXT field, insert data into it, and then use UPDATE to copy the data from the TEXT field to JSON, then clear the TEXT field through UPDATE.
When I insert the serialize data in the postmeta table the data is modified while inserting like:
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
but when I print this data before inserting it is displaying this that is right:
a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
while inserting it is automatically add s:107:" in front of data or "; in back of data.
Can any one please tell me why it is inserting like this.
Thanks in Advance
The s means string.
107 stands for the size of the string.
I run $wpdb->insert($table, $data) where data is an array with column_name => value and the insert is not working. I tried $wpdb->last_query and something bizarre comes back:
SHOW FULL COLUMNS FROM `table_im_trying_to_insert`
Why is the last query not my insert?
I found the problem. Apparently with the new WP update if you try to insert into a VARCHAR column and the column length is less than what you are trying to insert it just won't work. Prior to this update it will insert it but trim off the excess characters.
For me, I changed the field type from VARCHAR to TEXT, but it still didn't work. Finally, I found the table with collation of utf8_general_ci does not support emoji, so I removed all the emojis from the content then it works.
I have an insert:
$sql = 'INSERT into orders SET
fax_int_prefix = "'.$_SESSION['fax_int_prefix'].'",
fax_prefix = "'.$_SESSION['fax_prefix'].'",
fax_first = "'.$_SESSION['fax_first'].'",
fax_last = "'.$_SESSION['fax_last'];
The value of all of these fields is that they are blank right before the insert. Here is an example of one of them I echo'd out just before the insert:
$_SESSION[fax_prefix] =
For some reason it inserts the integer 0, instead of a blank value or null, as it should. Why is it inserting 0's instead of blank spaces into my DB?
Check the structure of your table. It's possible that it is not of type char or varchar (or that it is and has a default value set to '0'). To do this you can use phpmyadmin, SQLyog or other MySql admin programs.
Edit: If you want to store integers, but have the option of no value then make sure the type is nullable. i.e.: column_name INT(n) DEFAULT NULL
Check the schema of your database. Most likely they are integer fields or such.
SHOW FIELDS FROM orders;
or
SHOW CREATE TABLE orders;
might help. Additionally just printing out a variable is no good check, rather use var_dump().