如何使用 php 在 postgres 中插入 null

how to insert null in postgres with php

我正在尝试使用 php 将 null 插入到 postgres 数据库中。假设我有这个:

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

我尝试将 $param 设置为:

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

并且在每种情况下我都收到了这个错误:

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

我需要设置什么?

$param = 'null';

应该可以。

您可以使用 PDO 来完成:

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

Execute PHP online