简体   繁体   中英

PDO is inserting "null" for empty values

I am using PDO for writing to sqlite database.

// $dbo is an instance of PDO

$query     = "INSERT INTO items (id, name, rank) VALUES (:id, :name, :rank)";
$statement = $dbo->prepare($query);

$values = array('id' => 1, 'name' => 'Some Name', 'rank' => '');
 
$statement->execute($values);

$values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);
 
$statement->execute($values);

After executing any of these, I expect "rank" to be an empty string or even a NULL is acceptable. But what I get in the database is 'null' , yes a string 'null' value, not the real NULL .

Could not find any solution after several attempts.

Try sending NULL directly in this line

  $values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM