问题
I've seem this question a million times, but everyone seems to want to solve the problem in the database. I do not. I'm getting this error when parsing a large text file, picking out what I need and inserting it into my database. Out of 24 thousand rows or so, 30 or so have invalid characters in them.
Here is an example of the error, followed by the query that caused it:
[Query Error: Incorrect string value: '\xEF\xBC\x89' for column 'company' at row 1]
[INSERT INTO mac_address_db_new (hex,company) VALUES('0847D0','Nokia Shanghai Bell Co. Ltd.)')]
The 'bad' characters in the string I'm inserting are probably not going to make it all the way to your browser, so you likely can't see the character.
But I'm looking for a simpler solution. Is there a way I can filter out these characters in PHP before I try to send them to MySQL? I already filter with the following functions: trim( )
, mb_convert_encoding( )
(to UTF-8), htmlentities( )
, ~ some case-related functions ~, and finally mysqli_real_escape_string( )
. But still bad characters are getting through. I have no control over the text file input.
回答1:
I finally found a solution. I ditched the mb_convert_encoding( )
function and instead used iconv( )
, specifically this:
$output = iconv( 'UTF-8', "ISO-8859-1//IGNORE", $input );
Apparently the "//IGNORE" part is very important. This quietly strips these 'bad' characters out of my string before I send it off to the database.
来源:https://stackoverflow.com/questions/50074737/cleaning-sql-incorrect-string-value-error-from-php