gpt4 book ai didi

php - 带有字符串插值的准备语句因语法错误而失败

转载 作者:行者123 更新时间:2023-11-29 14:07:43 25 4
gpt4 key购买 nike

我正在尝试使用准备好的语句通过 PHP 和 Postgres 进行简单的插入。到目前为止,我已经这样做了:

<?php
$conn_string = "host=localhost port=5432"; // plus us/pw
$dbconn = pg_connect($conn_string);

$table = 'business_primary_category';
$column = 'primary_category';
$tag = 'restuarant';


// Prepare a query for execution
$result = pg_prepare($dbconn, "my_query", 'SELECT * FROM $table WHERE $column = $1');

// Execute the prepared query. Note that it is not necessary to escape
// the string "Joe's Widgets" in any way
$result = pg_execute($dbconn, "my_query", array("$tag"));

?>

我基本上是从 php.net 上的页面上获取的,但无法弄清楚我做错了什么。我是否需要安装一个库才能使用它或其他东西。谢谢!

这些是我得到的错误:

Warning: pg_prepare() [function.pg-prepare]: Query failed: ERROR: syntax error at or near "$" at character 15 in /home/url    **......**   pdo.php on line 11

Warning: pg_execute() [function.pg-execute]: Query failed: ERROR: prepared statement "my_query" does not exist in /home/url **......** pdo.php on line 15

Warning: pg_execute() [function.pg-execute]: Query failed: ERROR: prepared statement "my_query" does not exist in /home/url **......** pdo.php on line 18

最佳答案

如果你想让你的变量被插值,那么你需要使用双引号;否则 PHP 将其视为字符串文字。尝试:

$result = pg_prepare($dbconn, "my_query", "SELECT * FROM $table WHERE $column = $1");

关于php - 带有字符串插值的准备语句因语法错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466523/

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