I'm trying to insert a record in an automobile table. Here's a trimmed down version of the table.
NOTE: this is not a real life working model, should not be used or evaluated as such. I'm just showing the problem I'm having in a simplified way.
mysql> describe desc_autos; +-----------------+------------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+------------------+------+-----+---------------------+----------------+ | car_id | int(10) unsigned | NO | PRI | NULL | auto_increment | | automobile_name | varchar(25) | NO | MUL | | | +-----------------+------------------+------+-----+---------------------+----------------+ 7 rows in set (0.00 sec) mysql> select count(*) FROM desc_autos WHERE automobile_name IS NULL; +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec) mysql> select count(*) FROM desc_autos WHERE automobile_name = ''; +----------+ | count(*) | +----------+ | 19322 | +----------+ 1 row in set (0.02 sec) +-------------+ CREATE TABLE `desc_autos` ( `car_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `automobile_name` varchar(15) NOT NULL, PRIMARY KEY (`car_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------------+ 1 row in set (0.00 sec) So you see the automobile_name field is NOT NULL, and blank values are being shown for many records. (Disregard field names - just accept there is a field with non-null values)
I am converting from native MySQL functions to PDO. If I insert a record with a non-blank automobile_name value, no problem - works fine. If automobile_name is blank, PDO balks.
$automobile_name = ''; try { $q = " INSERT INTO desc_autos ( automobile_name ) VALUES ( :automobile_name ) "; $stmt = $dbx_pdo->prepare($q); $stmt->bindParam(':automobile_name', $automobile_name, PDO::PARAM_STR); $stmt->execute(); return true; } catch(PDOException $err) { log_error(); return false; } } This results in the following error:
Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'automobile_name' cannot be null I thought I had to implicitly tell PDO to insert a NULL value, otherwise blank values would be inserted as... blank values.
Ex:
$stmt->bindValue(':automobile_name', !empty($automobile_name) ? $automobile_name : NULL, PDO::PARAM_STR);
I'm not needing to insert NULLs in this case - I need to drop a blank, non-null value in the given field.
The question:
Why is PDO treating a blank value as null?
IS NULLworked with mysql_query()? Because PDO doesn't really influence MySQL expressions. As for inserting, you're binding the value withPDO::PARAM_STR, which will turn NULL values into blank strings.$automobile_name = '';explicitly setting it to the empty string.$q = "INSERT INTO desc_autos (automobile_name) VALUES ( $automobile_name )";then withmysqli_query( $dbx, $q);If$automobile_namewas blank (or even null), it would be inserted as a blank value in MySQL.->bindValue(often better retains types), or adapt your table scheme (e.g.DEFAULT ""or something).automobile_nameto accept NULL values eliminated the error. I then converted existing blank values to NULL with anUPDATE. This initially appears to resolve the issue in my dev system. @MichaelBerkowski has a good answer too.