Re: PDO Mysql data truncation missing error

From: Date: Tue, 21 Aug 2012 05:08:13 +0000
Subject: Re: PDO Mysql data truncation missing error
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-48412@lists.php.net to get a copy of this message
Matt Pelmear wrote: > Hello, > > I'm trying to detect data truncation on insert to MySQL using PDO. > > As far as I can tell, this gets reported at least in some cases (ex: > http://drupal.org/node/1528628), but I have been unable to see > this > myself. > > The test table I'm using has a column that is VARCHAR(5): > > mysql> describe test; > +-------+------------------+------+-----+---------+-------+ > | Field | Type | Null | Key | Default | Extra | > +-------+------------------+------+-----+---------+-------+ > | id | int(10) unsigned | YES | | NULL | | > | data | varchar(5) | YES | | NULL | | > +-------+------------------+------+-----+---------+-------+ > 2 rows in set (0.00 sec) > > > My test script inserts a ten character string into the 5 character column: > > ==== PHP TEST CODE ==== > > $pdo = new PDO( > 'mysql:host=localhost;dbname=test', > 'username', > 'password' > ); > > $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); > > $retval = $pdo->query( 'INSERT INTO test (data) VALUES > ("1234567890")' > ); > > print_r( $retval ); > > ==== END TEST CODE ==== > > This results in a new row in the test table, truncated after > the 5th > character as expected, but the truncation is not reported. (running in > php 5.3.2 and 5.3.4) > Running the same query directly in the mysql command line shows a > warning as expected. > > Am I missing something simple here? > > Thanks, > Matt Caveat: I don't use PDO but maybe the PDO::ERRMODE_WARNING attribute may do what you want? Cheers -- David Robley I'm not rude, I'm "attitudinally challenged". Today is Pungenday, the 14th day of Bureaucracy in the YOLD 3178.

« previous php.db (#48412) next »