lucymtc
lucymtc

Reputation: 21

Problem inserting a long text in database column MYSQL PHP

I am having a problem inserting a long text (around 9000 characters) with an INSERT query in php. I have already tested changing the column type (TEXT, MEDIUMTEXT,LONGTEXT) even thought TEXT type should do for 9000 chars. Also tested with a text free of any special chars or quotes.

I print my query and looks ok so I copy and paste into phpMyAdmin and the row inserts correctly. So the problem is coming when I try to INSERT from my php class.

I tested with a smaller text and this seems to work ok. I really need to get this solved. If anyone has the solution please let me know. Thanks!

Upvotes: 1

Views: 9290

Answers (4)

Dilpazir
Dilpazir

Reputation: 1

you should use mysqli_real_escape_string for storing long texts...it will be like

$variable= mysqli_real_escape_string(connection_variable,user input long text);

now you can store the $variable in your database by insert query, you should be storing $variable in longtext field in the database

Upvotes: 0

lucymtc
lucymtc

Reputation: 21

I haven't yet found what is the problem inserting my long texts, but I have found a solution to turn around it, it is not very clean but at least it will work until I found the real problem, just in case anyone has the same issue this is what I did.

Split the text in peaces of 1000 chars, do my INSERT and the UPDATE the text field in the data base adding the peaces of text, so the code :

$textArray = str_split($text,1000);

foreach($textArray as $t){ $model = new Article_Model_UpdateText($id,$t); }

The query in Article_Model_UpdateText looks like this :

"UPDATE mg_articles SET text = CONCAT (text, '".$text."') WHERE idArticle = ".$id.";";

Hope this helps someone, thanks for all your replies.

Upvotes: 1

Álvaro González
Álvaro González

Reputation: 146450

My crystal ball suggests the issue may be related to max_allowed_packet:

http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet

(But it's just a blind shot.)

Upvotes: 0

Yoko Zunna
Yoko Zunna

Reputation: 1824

Try with the Datatype BLOB or LONGBLOB in mysql.

It will do your work.

Upvotes: 0

Related Questions