1

I want to build query using two array 1 for column and another for value. I have 3 integer columns in first array for which I don't want to store 0 as default so i want to set NULL, I want to check if any value entered for it, if not entered i want to set NULL for it . But when i create query i cant see NULL, i just see null replaced by blank. Seems like PHP is stripping out the NULL word ?

$set_null = array("asst_phone","mailing_zip","other_zip");

    foreach($columns as $key=>$col){
    if(in_array($col,$set_null) && $values[$key] == ""){
       $val[$key] = NULL;
    }
    else    
       $val[$key] = "'".$values[$key]."'";
    }
    $query_vals = "";
    foreach($val as $qval){
      if($qval == "") $query_vals .= NULL;
        $query_vals .= $qval;
            }
    echo $query_vals;

It generates query like below

INSERT INTO zoho_contacts (`contactid`,`smownerid`,`contact_owner`,`lead_source`,`first_name`,`last_name`,`accountid`,`account_name`,`email`,`title`,`department`,`phone`,`home_phone`,`other_phone`,`fax`,`mobile`,`date_of_birth`,`assistant`,`asst_phone`,`reports_to`,`smcreatorid`,`created_by`,`modifiedby`,`modified_by`,`created_time`,`modified_time`,`mailing_street`,`other_street`,`mailing_city`,`other_city`,`mailing_state`,`other_state`,`mailing_zip`,`other_zip`,`mailing_country`,`other_country`,`description`,`email_opt_out`,`skype_id`,`salutation`,`add_to_quickbooks`,`secondary_email`) values ('418176000000052001','418176000000047003','','','','','418176000000047027','','','','','02164 25452132','','','','9798659821','','',,'','418176000000047003','','418176000000047003','','','','','','','','','',,,'','','','','','Mr.','','')

My mysql column structure

column properties = NULL => Yes, DEFAULT => NULL, right ??

3 Answers 3

3

Replace

$val[$key] = NULL;

with

$val[$key] = 'NULL';
Sign up to request clarification or add additional context in comments.

5 Comments

MySQL is looking for the string, not a php variable.
if we enclose NULL with 'NULL' then it works for column with datatype varchar, for integer column it stores zero, which i dont want to.
@www.amitpatil.me: Does the integer column allow NULL? Or is the column defined in the database as providing a default value of 0 when no value is provided?
@David yes, I'm going to ask that!
@www.amitpatil.me: The "resulting query" in the question still shows no instance of NULL in the inserted values, which it would need. What is the resulting query when you try this approach of wrapping NULL in quotes? Can you show that?
0

Probably, a better idea to use mysql_escape_string for all values.

if($val != "")
$val= "".mysql_escape_string($val)."";
else
$val = "NULL";

Comments

0

In php, NULL needs to be quoted (if you want it to work in this query)

7 Comments

if i enclose NULL with 'NULL' then it works for column with datatype varchar, for integer column it stores zero, which i dont want to.
(regarding this answer) Not true. In PHP the value NULL is perfectly acceptable unquoted. The issue is that when inserting NULL into a string, the value gets converted to be a blank string, or ''. In order to get MySQL to insert a null value, the string value of "NULL" needs to be passed into the query.
@nageeb : then whats the solution ??
the solution is to pass 'NULL' into the sql query. $foo=NULL evaluates to null in php, but if you want to pass that null value into MySQL, then you need to pass the literal value of 'NULL' so it would have to be $foo = 'NULL'.
no i tried that.....it stores NULL in varchar column but for integer column it stores 0.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.