简体   繁体   English

将NULL值传递给mysql存储过程

[英]Passing NULL values into mysql Stored procedure

I am trying to pass a few NULL values to a stroed procedure but PHP always retains the varaibles to be empty strings '' and not NULL. 我试图将一些NULL值传递给存储过程,但PHP始终将变量保留为空字符串”而不是NULL。

My DB insert function looks like this. 我的数据库插入函数如下所示。 (I am using a Framework and ADODB so if it looks weird thats why). (我使用的是Framework和ADODB,所以如果看起来很奇怪,那就是为什么)。

function insert_emp($f_name, $last_name, $dob) {

   if(!isset($dob)){
      $dob = NULL;
   }

   $this->DB->setConnection("CALL insert_emp('$f_name', '$l_name', '$dob')");
}

I have ensured that $dob is being passed an empty string ''. 我确保$ dob传递了一个空字符串''。 But for some reason when it calls the stored proc it is not changed to NULL. 但是由于某种原因,当它调用存储的proc时,它不会更改为NULL。 dob is a datetime field and mysql complains that you can not enter an empty string as a DATETIME. dob是日期时间字段,mysql抱怨您不能输入空字符串作为DATETIME。 If I hard code the insert such as this 如果我硬编码这样的插入

 $this->DB->setConnection("CALL insert_emp('Test1', 'Test2', 'NULL')"); 

it will work. 它会工作。 Thanks for the help. 谢谢您的帮助。

NULL cannot be represented as a string. NULL不能表示为字符串。 So you should use a string literal null. 因此,您应该使用字符串文字null。

function insert_emp($f_name, $last_name, $dob = 'NULL') {
   $this->DB->setConnection("CALL insert_emp('$f_name', '$l_name', '$dob')");
}

I have set it as default value, and it should do what you wanted in the first place. 我已将其设置为默认值,并且它应该首先执行您想要的操作。

尝试这个

$this->DB->setConnection("CALL insert_emp('Test1', 'Test2', '')");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM