简体   繁体   中英

how to insert null in postgres with php

I'm trying to insert null into a postgres database with php. Say I have this:

<?php
$sql = <<<EOT
insert into
    t1
(c1, c2)
values
($param, 'test')
EOT;
?>

I tried setting $param to:

$param = null;
$param = '\N';

and in each case I got this error:

Native message: ERROR:  syntax error at or near ","

What do I need to set it to?

$param = 'null';

it should work.

You can do it using PDO:

$query = "INSERT INTO t1 VALUES(?,?)";
$insertStatement = $pdo->prepare($query);
$insertStatement->execute([null, 'test']);

Execute PHP online

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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