1

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!

14
  • We need more information. Are there any errors/warnings? Show us the relevant code ... Commented Feb 17, 2011 at 11:43
  • 1
    What is the problem? What happens? Commented Feb 17, 2011 at 11:45
  • You shouldn't be worried about special chars if you're using something like mysql_real_escape_string... Are you using this? Commented Feb 17, 2011 at 11:50
  • no, I have no errors or warnings,and yes, I use mysql_real_escape_string, but just to make sure it was nothing to do with the text I cleaned it form special chars and quotes Commented Feb 17, 2011 at 12:04
  • @lucymtc You still haven't said anything about your problem, except that you have it. You know what your problem is but we don't. Commented Feb 17, 2011 at 12:16

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try with the Datatype BLOB or LONGBLOB in mysql.

It will do your work.

3 Comments

OP already said that it works if he tries it with phpMyAdmin. So it is more likely a problem with his code.
The smallest of the data types mentioned in the question (MEDIUMTEXT) can hold 16,777,215 characters. What's your reason to switch to BLOB?
anyway BLOB and LONGBLOB don't work either. TEXT type really should work for this amount of chars
0

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.)

1 Comment

I have set max_allowed_packet to 32M but still not working, thanks anyway for your reply
0

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

1 Comment

Hi and welcome to Stack Overflow. Thanks for answering - it's really appreciated. Just so you know, we have heaps of really super new newbies here on S/O, so it's often a good idea to be really explicit in explaining why and how your example will solve the problem. Don't forget that what may be obvious to you, might not be to them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.