PHP adding gibberish to strings passed into mysql_query
in PHP, I'm using mysql_query($querystring)
to insert some information into a table.
However, when I look at the MySQL Query Logs, it shows that gibberish is getting added:
MySQL Log:
INSERT INTO database.table
SET
`col1` = '0^@0^@:^@0^@0^@:^@1^@8',
`col2` = '3^@8^@0',
`col3` = '3^@6^@8',
`col4` = '1^@2'
When I do a var_dump() on $querystring, everything looks fine, no ^@ are appended.
PHP var_dump() - Note: I just made up the 333 number.
string(333) "INSERT INTO database.table
SET
`col1` = '00:00:18',
`col2` = '380',
`col3` = '368',
`col4` = '12'"
However, the MySQL logs are showing the ^@. Is there any way to strip the ^@ from the $querystring?
The values stored in the database are '00:00:00' (the default value for that field), '3', '3', and '1'. Col1 is of type dat开发者_Go百科e, and Col2-4 are smallint(5).
What exactly happens to the table?
Maybe you just forgot the "," after col1
= '00:00:18' ?
Make sure your field type is VARCHAR
, or at least INT
for numbers. MySQL
could be messing with your strings to make them fit an unfit type.
Here is the fix I ended up using:
/*
Strip hidden gibberish characters inherited from the original .csv file
NOTE: Do a var_dump() to check for hidden gibberish characters, compare the string length from the var_dump() to an eye count of the character length that shows on the screen. Also, check MySQL Query Log to see what characters are being added during INSERT
*/
for($j=0; $j<strlen( $Parts[$i]);$j++)
{
if(is_numeric($Parts[$i][$j]) or $Parts[$i][$j]==':' or $Parts[$i][$j]=='.')
$stringholder.=$Parts[$i][$j];
}
$Parts[$i] = $stringholder;
$stringholder ="";
Not the cleanest, but gets the job done.
Ended up being a Encoding Issue.
Fix:
$string_input_to_mysql_query = iconv("UCS-2", "ISO-8859-1", $string_input_to_mysql_query);
精彩评论