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.
My DB insert function looks like this. (I am using a Framework and ADODB so if it looks weird thats why).
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 ''. But for some reason when it calls the stored proc it is not changed to NULL. dob is a datetim开发者_C百科e field and mysql complains that you can not enter an empty string as a 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. So you should use a string literal 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.
try this
$this->DB->setConnection("CALL insert_emp('Test1', 'Test2', '')");
精彩评论