tags:

views:

897

answers:

3

I am using Zend Framework with MySQL,Apache and Ubuntu 9.04.

I am trying to insert NULL values into database like this:

$personObj->setPersonId( '1' );
$personObj->setPersonEmail('NULL');
$personObj->save();

But 'NULL' is stored in database as string and not NULL.

When I use this:

$personObj->setPersonId( '1' );
$personObj->setPersonEmail(NULL);
$personObj->save();

But nothing happens and previous entry is unchanged.

What should I do to insert NULL values into MySQL?

A: 

I think putting NULL in quotes is what makes it look like a string.

I don't know about your method, but for a direct insert though a mysql INSERT command, the single quotes around a NULL are incorrect.

pavium
I have also tried this $personObj->setPersonEmail(NULL); But nothing happened. Previous entry is unchanged.
NAVEED
It depends **entirely** on how the `setPersonEmail()` method works, then, and whether it is valid for *updating* an entry.
pavium
Yes it updates. When I use this $personObj->setPersonEmail('[email protected]'); It replace previous database entry with [email protected]. But my problem is to remove previous entry and insert NULL if an user remove his/her email address.
NAVEED
+1  A: 

First thought would be straight passing in the null keyword, without quotes around it. As pavium said, the quotes around it turn it into a string.

Slokun
I have tried this $personObj->setPersonEmail(null); But no luck. Actually I am updating this entry. An email is already exists in database and I want to remove this and insert NULL.
NAVEED
Could try empty quotes, though that would likely just make it an empty string. Have you tried accessing the column more directly, rather than through the set function? ie:$personObj->personEmail = null;
Slokun
+4  A: 

If you are not modifying any of the values after they are assigned then

new $personObj->setPersonEmail(new Zend_Db_Expr('NULL'));
Gorilla3D
yes it is working.
NAVEED
This worked for me too, but I have to do `new Zend_Db_Expr('NULL')`
jakenoble