|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2005-04-04 16:52 UTC] gpd at gpdnet dot co dot uk
 Description:
------------
Using bindParam and named arguments in an insert statement or stored procedure call I can't find a way of inserting or updating fields with a null value.
I think I am using the correct code below according to the PDO documentation
I've tried various combinations of values and parameters, but I can't get it to insert a null value.
Reproduce code:
---------------
SQL:
create table test (c1 int, c2 int);
---------
PHP:
$cn = new PDO('odbc:DRIVER={Adaptive Server Anywhere 9.0};ENG=webroster;DBN=cct8740;CommLinks=TCP','dba','sql');
//sleep(30);
$dr = $cn->prepare('insert into test(c1,c2) values(:c1, :c2)');
$val1 = 1;
$val2 = 2;
$dr->bindParam(':c1',$val1,PDO_PARAM_INT);
$dr->bindParam(':c2',$val2,PDO_PARAM_NULL);
$dr->execute();
Expected result:
----------------
values 1 and null in the test table
Actual result:
--------------
values 1 and 2 in the test table
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 06:00:01 2025 UTC | 
It seems that you'll get NULL in table only when $val2 === NULL: --- $val1 = 1; $val2 = NULL; $dr->bindParam(':c1',$val1,PDO_PARAM_INT); $dr->bindParam(':c2',$val2,PDO_PARAM_NULL); $dr->execute(); ---