Avatar of lawrence_dev
lawrence_dev
 asked on

Fatal error: Uncaught exception 'PDOException' in PHP script

How do I fix this error?

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x92s Lim...' for column 'Warranty' at row 1'

I just moved to a new server.  The old server never had script issues.  The new server is throwing errors in my script.  My script updates to row 86 then stops.  I have changed the particular database column to utf8 and utf8mb4_bin and cannot get it to work
PHP

Avatar of undefined
Last Comment
Ovunc Tukenmez

8/22/2022 - Mon
zephyr_hex (Megan)

What is the database?

And what is the string that's causing the error?
Ray Paseur

You can use try{} catch{} blocks to trap the error.  If you visualize the PDOException, there will be a message value that can give you a better diagnostic message.  Possibly the database is using charset = utf8, when it needs to use utf8mb4.
lawrence_dev

ASKER
Ray,  
I changed the columns with errors collation to utf8mb4 and utf8.   How do I set the entire table to utf8mb4?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
lawrence_dev

ASKER
Disregard, I changed the entire database to utf8mb4 and still received the same error.
Kim Walker

Incorrect string value: '\x92s Lim...' for column 'Warranty' at row 1'
Are you attempting to include the backslash in the column value? Depending on the database you're working with (which was asked in the first comment but never answered), the backslash is probably the escape character. If you want to include it in the column value, you either need to change the escape character or escape it in the SQL statement. "\x" doesn't translate to any escapable character that I know of, so it is probably generating the error.
lawrence_dev

ASKER
Thanks for your help Kim!  I am using PHP Mysql with PDO.  I have run this same script for over a year with no issues.  My host forced me to move to a new dedicated server/data center and then I started receiving the error.  So I am wondering if it is because of  newer version of PHP or PDO or Mysql??  What is the best way to escape problematic characters like this?

Thanks again!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
lawrence_dev

ASKER
Kim, In addition, I am importing the products from a remote csv file...
Kim Walker

Take a look at the php addslashes function.
Ovunc Tukenmez

If you use parameters in your query, you won't face problems with the escaping and this would also be more secure.

PDOStatement::bindParam
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
lawrence_dev

ASKER
My question is, why cant they just leave stuff well enough alone???  I am contactly changing my working code to please somebody's fetish.  It just does not make sense.

I dont have time to rework the entire page of code.  Please advise the best way to formulate a regex statement to remove the following string:

\xBE  or  \xB5    If I can remove "\x" followed by letters and numbers then I can eliminate most of the errors...

Thanks for your help!!
lawrence_dev

ASKER
$FullDesc = preg_replace("/\x[^a-zA-Z0-9]{2}/", " ", $FullDesc);    

How do I correctly structure this regex?
Ovunc Tukenmez

Try this:

$FullDesc = preg_replace('#\\\x[a-z0-9]{2}#i', ' ', $FullDesc);

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
lawrence_dev

ASKER
jet-black,

Thanks for your help!!

here is my string
$FullDesc = preg_replace('#\\\x[a-zA-Z0-9]{2}#i', ' ', $FullDesc);

Here is the error
 Incorrect string value: '\xB0 swiv...' for column 'Description' at row 1

What is the issue?  I thought I had it addressed above??
lawrence_dev

ASKER
jet-black,

If is "\x" followed by two characters I need it gone.  How do I correctly structure?
Kim Walker

If is "\x" followed by two characters I need it gone.  How do I correctly structure?
That's a bit vague. A word space is considered a character. As is a period, et al.

Should it be \x followed by any two alpha-numeric characters? jet-black's code has an extra escape character. The first backslash escapes the literal backslash. The third backslash would attempt to escape the x which is not a special character and would probably throw an error. Do you have errors turned on? Try this
$FullDesc = preg_replace('#\\x[a-z0-9]{2}#i', ' ', $FullDesc);

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Ovunc Tukenmez

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.