# pdo: cannot send NULL values thru bound parameters.

- Thread: https://externals.io/message/15572
- Original archive: https://news-web.php.net/php.internals/15572
- Started by Thies C.Arntzen on 2005-03-24T11:19:02+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #15572 — Thies C.Arntzen — 2005-03-24T11:19:02+00:00

URL: https://externals.io/message/15572#15572

as we convert "incoming" zvals to strings in pdo_stmt.c "no matter what"...

$stmt = $this->prepareStatement('insert into bla (name) values (:name)');
$name = NULL;
var_dump($name); // $name is `NULL` 
$stmt->bindParam(':name', $name);
var_dump($name); // $name is an empty string

am i overlooking something obvious?

attached patch "fixes" it for me (NULL is a valid "value" for a string)...

re, tc


Index: pdo_stmt.c
===================================================================
RCS file: /repository/php-src/ext/pdo/pdo_stmt.c,v
retrieving revision 1.94
diff -u -w -r1.94 pdo_stmt.c
--- pdo_stmt.c  21 Mar 2005 00:29:06 -0000  1.94
+++ pdo_stmt.c  24 Mar 2005 11:13:31 -0000
@@ -253,7 +253,7 @@
        }
    }

-   if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_STR && 
param->max_value_len <= 0) {
+   if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_STR && 
param->max_value_len <= 0 && ! ZVAL_IS_NULL(param->parameter)) {
        convert_to_string(param->parameter);
    }

---

## #15575 — Marcus Boerger — 2005-03-24T11:28:18+00:00

URL: https://externals.io/message/15572#15575 · In reply to: #15572

Hello Thies,

  may i kindly ask you to provide a test case as a .inc file in pdo/tests.
I'll then look into the details?

Thursday, March 24, 2005, 12:19:02 PM, you wrote:

> as we convert "incoming" zvals to strings in pdo_stmt.c "no matter what"...

> $stmt = $this->prepareStatement('insert into bla (name) values (:name)');
> $name = NULL;
> var_dump($name); // $name is `NULL` 
> $stmt->bindParam(':name', $name);
> var_dump($name); // $name is an empty string

> am i overlooking something obvious?

> attached patch "fixes" it for me (NULL is a valid "value" for a string)...

> re, tc


> Index: pdo_stmt.c
> ===================================================================
> RCS file: /repository/php-src/ext/pdo/pdo_stmt.c,v
> retrieving revision 1.94
> diff -u -w -r1.94 pdo_stmt.c
> --- pdo_stmt.c  21 Mar 2005 00:29:06 -0000  1.94
> +++ pdo_stmt.c  24 Mar 2005 11:13:31 -0000
> @@ -253,7 +253,7 @@
>         }
>     }

> -   if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_STR && 
> param->max_value_len <= 0) {
> +   if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_STR && 
> param->max_value_len <= 0 && ! ZVAL_IS_NULL(param->parameter)) {
>         convert_to_string(param->parameter);
>     }




-- 
Best regards,
 Marcus                            mailto:mail@marcus-boerger.de

---

## #15573 — Jared Williams — 2005-03-24T11:32:40+00:00

URL: https://externals.io/message/15572#15573 · In reply to: #15572

> $stmt = $this->prepareStatement('insert into bla (name) 
> values (:name)'); $name = NULL; var_dump($name); // $name is 
> `NULL` $stmt->bindParam(':name', $name); var_dump($name); // 
> $name is an empty string
> 
> am i overlooking something obvious?

$stmt->bindParam(':name', $name, PDO_PARAM_NULL);

Or 

$stmt->bindParam(':name', $name, is_null($name) ? PDO_PARAM_NULL : PDO_PARAM_STR);

Is what I've been using, and works. http://pecl.php.net/bugs/bug.php?id=3391 was the original bug report I opened, that's since been
fixed.

Jared

---

## #15574 — Thies C.Arntzen — 2005-03-24T11:42:42+00:00

URL: https://externals.io/message/15572#15574 · In reply to: #15575

Am 24.03.2005 um 12:28 schrieb Marcus Boerger:

> Hello Thies,
>
>   may i kindly ask you to provide a test case as a .inc file in 
> pdo/tests.
> I'll then look into the details?
>

re, thies

---

## #15576 — Marcus Boerger — 2005-03-24T12:33:13+00:00

URL: https://externals.io/message/15572#15576 · In reply to: #15574

Hello Thies,

 thanks & committed

 marcus
 
Thursday, March 24, 2005, 12:42:42 PM, you wrote:
> Am 24.03.2005 um 12:28 schrieb Marcus Boerger:

> > […]

> re, thies

---

## #15577 — Dan Scott — 2005-03-24T12:49:44+00:00

URL: https://externals.io/message/15572#15577 · In reply to: #15576

I have updated the PDOStatement::bindParam() documentation to include
an explicit example of how to pass a `NULL` value.

Thanks for the suggestion and question!

Dan

On Thu, 24 Mar 2005 13:33:13 +0100, Marcus Boerger
<mail@marcus-boerger.de> wrote:
> Hello Thies,
> 
>  thanks & committed
> 
>  marcus
> 
> Thursday, March 24, 2005, 12:42:42 PM, you wrote:
> > […]
> 
> > […]
> 
> > […]
