Menu

MySQL and PHP – insert NULL rather than empty string

Written by

To pass a NULL to MySQL, you do just that.

$notes = !empty($notes) ? "'$notes'" : "NULL";

$query = "INSERT INTO data (name, notes)  VALUES ('name',$notes)";

or you could add condition like…

$query = "INSERT INTO data (name, notes) VALUES ('name',NULLIF('$notes',''))";

 

Article Categories:
PHP

Comments are closed.

Shares