[英]PDO Statement Error: 42601 syntax error near “$6”
I'm having trouble getting this simple query to execute. 我无法让这个简单的查询执行。 I'm using nameless parametized queries and Postgres + PostGIS.
我正在使用无名参数化查询和Postgres + PostGIS。 The PDO Statements error info is as follows:
PDO语句错误信息如下:
Array
(
[0] => 42601
[1] => 7
[2] => ERROR: syntax error at or near "$6" at character 28
)
Which is a syntax error in PostGreSQL, which makes me feel silly that I cannot find it... 这是PostGreSQL中的语法错误,这让我感觉很傻,我找不到它...
Here is the query in question 这是有问题的查询
$sql = "SELECT ?,?,?,?,?
FROM ?
WHERE ST_Distance((SELECT the_geom FROM polyon_table WHERE gid = ? ), ?.the_geom) < 0.1";
Then I prepare it... $stmt = $dbh->prepare($sql);
然后我准备它...
$stmt = $dbh->prepare($sql);
. 。 The errorInfo from $dbh gives a code 00000 (success).
来自$ dbh的errorInfo给出了代码00000(成功)。
The $params
is as follows: $params
如下:
array(8) {
[0]=>
string(4) "area"
[1]=>
string(9) "perimeter"
[2]=>
string(6) "lat"
[3]=>
string(6) "lon"
[4]=>
string(8) "data"
[5]=>
string(10) "myTable"
[6]=>
int(8)
[7]=>
string(10) "myTable"
}
Then I execute and pass it the $params array
. 然后我执行并传递
$params array
。 $stmt->execute($params)
and I print the error code and get this: $stmt->execute($params)
我打印错误代码并得到:
Array
(
[0] => 42601
[1] => 7
[2] => ERROR: syntax error at or near "$6" at character 28
)
The $stmt->debugDumpParams();
$stmt->debugDumpParams();
is as follows: 如下:
SQL: [112] SELECT ?,?,?,?,? FROM ? WHERE ST_Distance((SELECT the_geom FROM polygon_table WHERE gid = ? ), ?.the_geom)< 0.1
Params: 8
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
...
// shortened it to save space.... all of them look the same, except 6, which has param type = 1
Key: Position #7:
paramno=7
name=[0] ""
is_param=1
param_type=2
So, after trying a few things (looping and binding the params individually, changing the int(8) in $params
to a string, etc). 因此,在尝试了一些事情之后(单独循环和绑定params,将
$params
的int(8)更改为字符串等)。 I copied and pasted the sql into the terminal to run the query. 我将sql复制并粘贴到终端中以运行查询。 I replaced all the variables very carefully and in the exact order of
$params
and it runs fine. 我非常仔细地更换了所有变量,并且按照
$params
的确切顺序,它运行正常。
My conclusion is that the sixth variable(the integer) isn't getting inserted properly. 我的结论是第六个变量(整数)没有正确插入。 Any ideas why this is happening?
任何想法为什么会这样?
Hopefully, this sort of error isn't a repeat question. 希望这种错误不是一个重复的问题。 I went through 3 pages of questions.
我经历了3页的问题。
You can use query parameters to substitute for literal values, but not for table names, column names, lists of values, expressions, SQL keywords, etc. 您可以使用查询参数替换文字值,但不能替换表名,列名,值列表,表达式,SQL关键字等。
Think of it this way: at prepare
time, the SQL parser must know all the syntax your query uses, and also which tables and columns you're using. 可以这样考虑:在
prepare
时,SQL解析器必须知道您的查询使用的所有语法,以及您正在使用的表和列。 Otherwise how would it know that those tables and columns exist in your database, or if it should give you an error? 否则,它会如何知道您的数据库中是否存在这些表和列,或者它是否应该给您一个错误? It is supposed to give you any errors related to non-existant tables at prepare time, not at execute time.
它应该在准备时给你任何与不存在的表有关的错误,而不是在执行时。
So when you provide a string "myTable" for your 6th parameter value, it's as if you execute a query: 因此,当您为第6个参数值提供字符串“myTable”时,就像执行查询一样:
SELECT ... FROM 'myTable' ...
That is, instead of a table name, it's a quoted string literal. 也就是说,它不是表名,而是引用的字符串文字。 That doesn't make any sense in SQL;
这在SQL中没有任何意义; it's an error of syntax and semantics.
这是语法和语义的错误。
I'm not entirely sure, but I don't think that you can parametize the table name, I'm not even sure that you can for the columns too, but I haven't tested it. 我不完全确定,但我不认为你可以参数化表名,我甚至不确定你是否可以用于列,但我还没有测试过它。
Also, not sure that ?.column
works. 另外,不确定
?.column
工作原理。 I don't think that it just replace the parameters with strings. 我不认为它只是用字符串替换参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.