I am parsing an XML feed into a MYSQL table using simplexml_load_file(). Some of the variables are blank, I would like them to be actual NULL instead of NULL, or is there even a difference?
$xml = simplexml_load_file('cb.xml') or die ('Bad XML File');
foreach($xml->item as $item) {
$name = $item->name;
//Tried
if ($name == '') {
$name = 'NULL';
}
//And
if ($name == '') {
$name = NULL;
}
mysql_query("INSERT INTO cb (name) VALUES ('$name')");
This is because you’re giving MySQL a string:
And the PHP
nullvalue, when “casted” to a string, becomes an empty string. So your first try gave... ('NULL'), and now it gives... ('').You must use the
NULLkeyword inside the query, without quotes, to insertNULLinto a database field.Oh, and as usual, take care not to get SQL-injected with your unprotected
$namevariable.