PDO没有绑定变量

时间:2014-01-19 11:28:47

标签: php mysql sql pdo

下面的代码返回一个空数组。运行SQL查询时,它返回超过40行。现在已经被困在这一个上超过一个小时了,并且还想把我的头发拉出来。

我尝试过的一件事是手动连接get变量,然后查询 成功。基于此,我认为问题与绑定有关。

$_GET['ne'] = '53.23514382039281 -1.4177794752807813';
$_GET['sw'] = '53.23265539538397 -1.4263625441284375';

<?php
ini_set("display_errors",1);
require_once('../../includes/database.php');

try {
    $stmt = $conn->prepare("SELECT X(Coordinates) AS `latitude`, Y(Coordinates) AS `longitude`
                            FROM stop
                            WHERE MBRContains(
                                GeomFromText( 'LINESTRING(:ne,:sw)' ),
                                Coordinates)");

    $stmt->bindParam('ne', $_GET['ne'], PDO::PARAM_STR, 255);
    $stmt->bindParam('sw', $_GET['sw'], PDO::PARAM_STR, 255);

    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    echo $e->getMessage();
}

echo json_encode($results);

?>

3 个答案:

答案 0 :(得分:3)

$stmt->bindParam(':ne', $_GET['ne'], PDO::PARAM_STR, 255);
$stmt->bindParam(':sw', $_GET['sw'], PDO::PARAM_STR, 255);

您必须执行语句才能获得结果

$ stmt-&GT;执行();查看更多信息here

答案 1 :(得分:3)

您无法绑定引用字符串'LINESTRING(:ne,:sw)'中的参数。 如果你坚持使用绑定,那么尝试这样做。

GeomFromText( concat('LINESTRING(',:ne,',',:sw,')') )
你应该得到这个错误。

警告:PDOStatement :: execute()[pdostatement.execute]:SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

添加此行

error_reporting(E_ALL | E_STRICT);

之后

ini_set("display_errors",1);

答案 2 :(得分:1)

<?php $stmt->execute() // missing
?>
相关问题