Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO PostgreSQL无法确定数据类型_Php_Postgresql_Pdo_Prepared Statement - Fatal编程技术网

PHP PDO PostgreSQL无法确定数据类型

PHP PDO PostgreSQL无法确定数据类型,php,postgresql,pdo,prepared-statement,Php,Postgresql,Pdo,Prepared Statement,在使用带有PostgreSQL的PHP PDO进行特定查询时,我遇到了一个奇怪的错误,我不明白为什么 我有以下代码 $conn = new PDO($db, $us, $pw); try { $count = $conn->prepare("select count(idno) as count from t_dummy where ( ? is null or idno = ? )"); $count->execute([null, null]);

在使用带有PostgreSQL的PHP PDO进行特定查询时,我遇到了一个奇怪的错误,我不明白为什么

我有以下代码

$conn = new PDO($db, $us, $pw);

try {
   $count = $conn->prepare("select count(idno) as count from t_dummy where ( ? is null or idno = ? )");
   $count->execute([null, null]);
   $count_fetch = $count->fetch();
} catch(Exception $error) {
   echo $error;
}

var_dump($count_fetch[0]);
如果PDO连接到MySQL数据库,那么它可以正常工作,但是当连接到PostgreSQL数据库时,它会失败,并出现以下错误

could not determine data type of parameter $1
看起来它不喜欢为
获取参数?为null,但我不知道为什么,因为这是有效的PostgreSQL语法,在PgAdmin中可以正常工作

使用CAST():


有什么意义?是否为空
?您希望它做什么?这是一种处理空id的方法,如果id参数为空,则第一个条件为真,并对所有记录计数,如果id不为空,则第二个条件仅对与id号匹配的记录为真在查询中使用CAST(),例如:CAST(?AS integer)谢谢@FrankHeikens这正是我所需要的,你能给我一个答案让我接受吗?
$conn = new PDO($db, $us, $pw);

try {
   $count = $conn->prepare("SELECT COUNT(idno) AS count FROM t_dummy WHERE ( CAST(? AS integer) IS NULL OR idno = ? );");
   $count->execute([null, null]);
   $count_fetch = $count->fetch();
} catch(Exception $error) {
   echo $error;
}

var_dump($count_fetch[0]);