Problem inserting a long text in database column MYSQL PHP - 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!

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.

Try with the Datatype BLOB or LONGBLOB in mysql.
It will do your work.

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

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

Related

MySQL DELETE doesn't work when value has hyphen

I've found that a hyphen in a value in a MySQL DELETE statement from PHP tends to make the thing just... not work. It doesn't fail, but it also doesn't delete the proper rows. Why exactly is this? INSERT works fine, it's just DELETE.
Edit: The particular query I'm running in this case (with the IP removed) is:
DELETE FROM ratings WHERE ip=IP_ADDRESS AND video = '-yfOsVrJLGt'
This runs successfully when I remove the hyphen.
I've also added that this occurs in PHP, and is seemingly limited to such.
Table structure, as per request:
There are currently no records in it.
Your video column length is varchar(10), but passing value contains 11
video = '-yfOsVrJLGs'
check your video length.varchar(10) but -yfOsVrJLGs contain 11 character.So try to give correct input
DELETE FROM ratings WHERE ip=IP_ADDRESS AND video = 'yfOsVrJLGs'
Very simple, but easily to make the mistake.
Your length of the column is varchar(10) and but needs to be varchar(11) as there are 11 characters. video = '-yfOsVrJLGs'

Codeigniter db class truncating text on insert

I have a codeigniter controller that receives json content from an API and inserts it to a mysql longtext field unchanged.
Everything's been running smoothly for a while, but recently i've been noticing some of the content is being truncated. Here are two examples.
The table being inserted has three fields
id int
data longtext
date_added datetime
I am receiving the data and directly adding it to the database using the active record insert function. It looks like that
$this->db->insert('received', array('data' => $data, 'date_added' => date('Y-m-d H:i:s')));
Using the profiler, i monitored the queries and found two faulty ones:
INSERT INTO `received` (`data`, `date_added`) VALUES ('{\"status\":{\"lastMaintenanceAt\":0000,\"code\":304,\"period\":900},\"items\":[{\"permalinkUrl\":\"http://example.com\",\"updated\":0000,\"postedTime\":0000,\"summary\":\"Man\'s guarantees are like his words.\",\"title\":\"By: John\",\"content\":\"<p>Man’s guarantees are like his words.</p>\",\"id\":\"http://example.com\",\"actor\":{\"permalinkUrl\":\"\",\"displayName\":\"John\"}}],\"title\":\"Comments on: Banker refuses to give ‘Written Guarantee’\"}', '2012-04-08 00:28:29')
and
INSERT INTO `received` (`data`, `date_added`) VALUES ('{\"status\":{\"code\":304,\"period\":900,\"feed\":\"http://example.com\"},\"items\":[],\"title\":\"Comments on: Making her cry…\"}', '2012-04-08 00:49:35')
The queries seems alright. But only part of the JSON is making it to the table in the first case, it is truncated after "[...] refuses to give" and in the second after making her cry.
The queries are not returning an error and the date is being inserted properly. Moreover, if i copy the queries and execute them in a mysql command prompt, they will insert the full text.
These queries a one a few of hundreds of other successful ones.
Any idea what might be the problem?
thank you
All of the faulty queries share one thing in common: they've got "special characters" which MS Word inserts, e.g. ‘, ’ and …. If you can convert / remove these, then your problems should go away.
Update
For a CodeIgniter solution to this, you could load the Text Helper library and then use the ascii_to_entities function on any strings, e.g.
$this->load->helper('text');
$desc = ascii_to_entities($desc);

TCPDF problems part 2

I am exporting data into a pdf using TCPDF. Everything works fine until I add a certain column (long text format) to the table. Whenever I add it, the table doesn't show up. When I run the sql query all the data shows up fine.
Is it possible there's a character or characters in the data field itself that are causing the table to become corrupted?
Also I can't for the life of me, figure out how I show more than 256 characters in cell.
If anyone can help I'd really appreciate it.
well until I find a better option I am running this to each of my comment fields
UPDATE TABLE_NAME set COLUMN_NAME = replace(COLUMN_NAME, '’', '`');

How do I find records when data entry has been inconsistent?

A group of people have been inconsistently entering data for a while.
Some people will enter this:
101mxeGte - TS 200-10
And other people will enter this
101mxeGte-TS-200-10
The sad thing is, those are supposed to be identical records.
They will also search inconsistently. If a record was entered one way, some people will search the other way.
Now, I know all about how you can fix data entry for the future, but that's NOT what I am asking about. I want to know how it is possible to:
Leave the data alone, but...
Search for the right thing.
Am I asking for the impossible here?
The best thing I found so far was a suggestion to simply muck about with the existing data, using the REPLACE function in mySQL.
I am uncomfortable with this option, as it means it will certainly actively piss off half of the users. The unfocused angst of all is less than the active ire of half.
The problem is that it has to go both ways:
Entering spaces in the query has to find both space and not-space entries,
and NOT entering spaces ALSO has to find both space and not-space entries.
Thanks for any help you can offer!
The "ideal" solution is pretty straightforward:
Decide what is the canonical way of representing a record
When someone saves a record, canonicalize it before saving
When someone searches for a record, canonicalize the input before searching for it
You could also write a small program to convert all existing data to the canonical form (you will have the code for it anyway, as "canonicalize" in steps 2 and 3 require that you write code that does so).
Edit: some specific information on how to canonicalize
With the sample data you give, the algorithm might be:
Replace all spaces with hyphens
Replace all runs of one or more hyphens with a single hyphen (a regex would be easiest for this -- actually, a regex can do both steps in one go)
Is there any practical problem with this approach?
Trim whitespaces from BOTH the existing data and the input of the search. That way the intended record(s) will always be returned. Hope your data size is small, though, because it's going to perform pretty poorly.
Edit: by "existing data" I meant "the query of existing data". My answer was based on assumption that the actual data could not be touched (which might not be correct).
If it where up to me, I'd have the data in the database updated with REPLACE, and on future searches when dealing with the given row remove all spaces in the input.
Presumably your users enter the search terms (or record details, when creating a record) in an HTML form, which then goes to a PHP script. It looks like your data can always be written in a way that contains no spaces, so why don't you do this:
Run a query that strips spaces from the existing data
Add code in the PHP script(s) that receives the form(s), so that it strips spaces from submitted data - whether that data is to be used for search or for writing new data.
Edit: I guess you would also need to change some spaces to hyphens. Shouldn't be too hard to write logic to accomplish that.
Something like this.
pseudo code:
$myinput = mysql_real_escape_string('101mxeGte-TS-200-10')
$query = " SELECT * FROM table1
WHERE REPLACE(REPLACE(f1, ' ', ''),'-','')
= REPLACE(REPLACE($myinput, ' ', ''),'-','') "
Alternatively you might write your own function to trim the data so it can be compared.
DELIMITER $$
CREATE FUNCTION myTrim(AStr varchar) RETURNS varchar
BEGIN
declare Result varchar;
SET Result = REPLACE(AStr, ' ','');
SET Result = ......
.....
RETURN Result;
END$$
DELIMITER ;
And then use this in your select
$query = " SELECT * FROM table1
WHERE MyTrim(f1) = MyTrim($myinput) "
have you ever heard of SQL's LIKE?
http://dev.mysql.com/doc/refman/4.1/en/string-comparison-functions.html
there's also regex
http://dev.mysql.com/doc/refman/4.1/en/regexp.html#operator_regexp
101mxeGte - TS 200-10
101mxeGte-TS-200-10
how about this?
SELECT 'justalnums' REGEXP '101mxeGte[[:blank:]]*(\-[[:blank:]]*)?TS[[:blank:]-]*200[[:blank:]-]*10'
digits can be represented by [0-9] and alphas as [a-z] or [A-Z] or [a-zA-Z]
append a + to make then multiple of that. perens allow you to group and even capture what is in the perens and reuse it later in a replace or something else.
RLIKE is the same as REGEXP.

PHP Imported CSV data cut short when encounterng a comma

I have a simple PHP script that loops over data in a CSV file, and adds the records to the database accordingly. One of my fields is a description field, but that description field itself has a comma (or multiple comma's) in it. It seems as though data for that field is only read up until the comma, however the next field is correct, so it is not as though the field after that is the remainder of the description, is is using the next column which is right.
Am I supposed to escape the comma? I am adding this data to a MySQL database, could that be where the issue is being caused?
My SQL query could be something like:
$description = $data[7]; //description column eg: "hello, my name is xxxxx, I am old"
INSERT INTO tblsomething (id, description) VALUES ($id, '$description');
The above statement only inserts the description as "hello" and nothing after the first comma it encounters.
Any ideas why this is?
Many thanks,
Simon
EDIT: This is solved, apologies to all as it was a silly mistake. It appears that the person who did the front end was creating arrays of content using the patter ',' to split the content. IT seems that the description - although supposed to be one array entry - was being split into multiple entries due to it containing comma's. This will be solved by using a more rare character like the pipe symbol to create our separators.
Thanks to all
Because it's not a CSV file. Fields in a CSV file that contain commas are supposed to be delimited by double quotes; this way the CSV functions in PHP will handle them properly.

Categories