Sending NULL values to MySQL through PHP

Had a need to send null values through PHP today. Finally figured it out. If I had the following query

$qry = INSERT INTO company_tb (cname,address,city) values (‘$name’,'$address’,$city)

and $city was my variable to pass the city value, then I would do the following

if ($city==”") //if $city is empty

$city = “NULL”; //set it as NULL

else $city = “‘” . $city . “‘”; //else set it as the city name plus single quotes around it. So if $city had Karachi, $city would become ‘Karachi’

So the query would become INSERT INTO company_tb (cname,address,city) values (‘Aqueeb’,'test address’,'Karachi’) if there was a city value otherwise it would become INSERT INTO company_tb (cname,address,city) values (‘Aqueeb’,'test address’,NULL)

Hope this helps someone! If you have any questions please feel free to ask them!

Tags:

2 Responses to “Sending NULL values to MySQL through PHP”

  1. Robor says:

    Greatings, Interesting, I`ll quote it on my site later.
    Robor

  2. SonyaSunny says:

    achnid.com – da best. Keep it going!
    SonyaSunny

Leave a Reply