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

Leave a Reply

Your email address will not be published. Required fields are marked *

Shares