简体   繁体   English

PHP PDO查询未插入 - 错误HY093

[英]PHP PDO query not inserting - Error HY093

After a lot of searching the web, the times I see this error, it looks really scenario specific. 经过大量的网络搜索,我看到这个错误的时候,它看起来确实特定于场景。 So far, I haven't found one that matched my scenario. 到目前为止,我还没有找到一个符合我情景的产品。 I think my issue is coming from a prepared statement with spatial data type params. 我认为我的问题来自于空间数据类型参数的预备声明。

The way I'm executing my code is: 我执行代码的方式是:

$sql = $conn->prepare("INSERT INTO states(`name`, `poly`) VALUES(':name',GeomFromText('GEOMETRYCOLLECTION(:coords)'));");
$res = $sql->execute(['name'=>$name, 'coords'=>$coords]);

if($res){
    echo "... Successfully Inserted<br><br>";
}
else{
    echo "... Failed<br><br>";
    print_r($sql->errorInfo());
    echo "<br><br>";
}

The above is failing. 以上是失败的。 The connection to the database has been tested. 已测试与数据库的连接。 Since these are rather large geometry sets, instead of pasting my code, I'll show how I verified my SQL: 由于这些是相当大的几何集,而不是粘贴我的代码,我将展示我如何验证我的SQL:

Dumping a raw SQL file and copy/pasting the SQL into a phpMyAdmin window, everything inserted just fine. 转储原始SQL文件并将SQL复制/粘贴到phpMyAdmin窗口,所有内容都插入正常。

$sqlStr = "INSERT INTO states(`name`, `poly`) VALUES('$name',GeomFromText('GEOMETRYCOLLECTION($coords)'));";
$check = file_put_contents('./states/'.$name.'2.sql', $sqlStr);

So it's because of this, that I believe my sql is correct, but it my problem is likely due to the prepare/execute portion somehow. 因此,我认为我的sql是正确的,但我的问题可能是由于准备/执行部分不知何故。 I'm not sure if spatial data types can't be assigned like this? 我不确定是否不能像这样分配空间数据类型?

Edit 编辑

I also want to note that I am on PHP version 5.5.9 and I've executed queries in the original method, with the params in the execute just fine. 我还想注意我在PHP版本5.5.9上并且我在原始方法中执行了查询,执行中的参数就好了。

There's no way the code at the end could be working. 最终的代码无法正常工作。 Parameters in the query must not be put inside quotes. 查询中的参数不能放在引号内。

Since GEOMETRYCOLLECTION(:coords) has to be in a string, you need to use CONCAT() to create this string. 由于GEOMETRYCOLLECTION(:coords)必须在字符串中,因此需要使用CONCAT()来创建此字符串。

$sql = $conn->prepare("
    INSERT INTO states(`name`, `poly`) 
    VALUES(:name,GeomFromText(CONCAT('GEOMETRYCOLLECTION(', :coords, ')')));");

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

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