简体   繁体   English

如何使用 php 在 postgres 中插入 null

[英]how to insert null in postgres with php

I'm trying to insert null into a postgres database with php.我正在尝试使用 php 将 null 插入到 postgres 数据库中。 Say I have this:说我有这个:

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

I tried setting $param to:我尝试将 $param 设置为:

$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:您可以使用 PDO 来做到这一点:

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

Execute PHP online 在线执行PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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