gpt4 book ai didi

PHP PDO PostgreSQL -> 插入/更新日期或时间戳数组,以及 bool 值

转载 作者:行者123 更新时间:2023-11-29 12:16:28 25 4
gpt4 key购买 nike

我正在尝试使用 PDO 使用 PHP 更新 PostgreSQL 数据库。

这 2 列的类型为 bool[] 和 timestamp[],均为 ARRAYS。

bool 数组出现此错误:

exception: PDOException: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type boolean: "'true','true','true','true','true'" in /var/www/fussyfindings.com/public_html/dbtest.php:49 Stack trace: #0 /var/www/fussyfindings.com/public_html/dbtest.php(49): PDOStatement->execute() #1 {main}

And a similar one for the date.

I have spent 2 days trying to fix it and I don't know how.

If I manually run a query on the server, it works fine with similar syntax.

UPDATE ff_search_data
SET someboolarray = ARRAY['true','true','true','true','true']::bool[], sometsarray = ARRAY['2009-12-01 00:00:00','2009-12-01 00:00:00','2009-12-01 00:00:00','2009-12-01 00:00:00','2009-12-01 00:00:00','2009-12-01 00:00:00']::timestamp[]
WHERE search_term = 'test';

然后数据库包含:

{true,true,true,true,true} and {'2009-12-01 00:00:00.000','2009-12-01 00:00:00.000','2009-12-01 00:00:00.000','2009-12-01 00:00:00.000','2009-12-01 00:00:00.000','2009-12-01 00:00:00.000'}

Can anyone help? I am very frustrated... I have tried putting { } around the implodes and it didn't make a difference.

Thank you.

Here's a code example.

<?php
require_once("../config.php");

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$servername = PQSQL_DB_HOST;
$database = PQSQL_DB_NAME;
$username = PQSQL_DB_USERNAME;
$password = PQSQL_DB_PASSWORD;
$sql = "pgsql:dbname=$database;host=$servername;user=$username;password=$password";
$dsn_Options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];

try {
$db_connection = new PDO($sql, $username, $password, $dsn_Options);
$db_connection->exec("SET NAMES 'UTF8';");
} catch (PDOException $error) {
http_response_code(503);
return false;
}

$boolvalue = "'true'"; // I want to use the string version, unless you can store the other in array below
$timestamp = date('Y-m-d H:i:s');
$searchterm = "test";

$boolarray = array();
$tsarray = array();

for ($i=0; $i<5; $i++) {
array_push($boolarray, $boolvalue);
array_push($tsarray, $timestamp);
}



$tsarray = implode(",", $tsarray);
$boolarray = implode(",", $boolarray);

$update_statement = $db_connection->prepare("UPDATE " . PQSQL_DB_TABLE_SEARCH_DATA . "
SET someboolarray = ARRAY[:bool_array]::bool[], sometsarray = ARRAY[:ts_array]::timestamp[]
WHERE search_term = :search_term");

$update_statement->bindParam(":bool_array", $boolarray, PDO::PARAM_BOOL);
$update_statement->bindParam(":ts_array", $tsarray);
$update_statement->bindParam(":search_term", $searchterm);
try {
if ($update_statement->execute()) {
echo "updated";
http_response_code(200);
return true;
}
else {
echo "update failed";
http_response_code(403);
return false;
}
}
catch (Exception $e) {
http_response_code(503);
echo "exception: " . $e;
return false;
}
?>

最佳答案

本质上,您试图将 implode 的结果绑定(bind)到 ARRAY[...] 的单个 strings(不是数组)接收多个逗号分隔的项目。考虑构建一个包含多个 qmarks 占位符 ? 的准备好的语句,然后将其与 for 循环迭代绑定(bind)。

数组构建

$boolvalue = "true";
$timestamp = date('Y-m-d H:i:s');
$searchterm = "test";

$boolqmarks = array(); # NEW Q MARK ARRAY
$tsqmarks = array(); # NEW Q MARK ARRAY

$boolarray = array();
$tsarray = array();

for ($i=0; $i<5; $i++) {
array_push($boolqmarks, '?');
array_push($boolarray, $boolvalue);

array_push($tsqmarks, '?');
array_push($tsarray, $timestamp);
}

SQL 参数化

# SINGLE STRINGS
$boolqmarkstr = implode(", ", $boolqmarks);
$tsqmarkstr = implode(", ", $tsqmarks);

# PREPARE STATEMENT WITH ? PLACEHOLDERS
$sql = "UPDATE mytable
SET someboolarray = ARRAY[". $boolqmarkstr ."]::bool[],
sometsarray = ARRAY[". $tsqmarkstr ."]::timestamp[]
WHERE search_term = :search_term";

$update_statement = $db_connection->prepare($sql);

# BIND ? AND NAMED PARAMS (bindValue is 1-indexed)
foreach (array_merge($boolarray, $tsarray) as $k => $v)
$update_statement->bindValue(($k+1), $v);

$update_statement->bindParam(":search_term", $searchterm);

# EXECUTE QUERY
$update_statement->execute();

关于PHP PDO PostgreSQL -> 插入/更新日期或时间戳数组,以及 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51801146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com