2012-04-21 220 views
1

我有一个PDO绑定参数的问题,当它在数据库上执行时,所设置的参数没有被推入到查询中。我通过让MySQL将查询记录到文件来目击此事。PHP PDO bindParam返回NULL

如果我对服务器执行 - 我在日志文件中看到这个。

-

SELECT sg.locID, (3959 * acos(cos(radians(53.21333)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-2.4354014)) + sin(radians(53.21333)) * sin(radians(latitude)))) AS distance FROM location_geo sg HAVING distance < 1000 ORDER BY distance LIMIT 0 , 20 

当从PHP/PDO内执行

SELECT 
    sg.locID, 
    (3959 * acos(cos(radians(NULL)) * cos(radians(latitude)) * cos(radians(longitude) - radians(NULL)) + sin(radians(NULL)) * sin(radians(latitude)))) AS distance 
FROM 
    location_geo sg 
HAVING 
    distance < 1000 
ORDER BY distance LIMIT 0, 20 

这里是代码多数民众赞成通过PHP

$lat = 53.21333; 
$lng = -2.4354014; 
$limit = 10; 
$start = 0; 

$query = " 
    SELECT 
     sg.locID, 
     (3959 * acos(cos(radians(:latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians(:longitude)) + sin(radians(:latitude)) * sin(radians(latitude)))) AS distance 
    FROM 
     location_geo sg 
    HAVING 
     distance < :radius 
    ORDER BY distance LIMIT :start, :limit 
"; 

$stm = Modules::db()->prepare($query); 
$stm->bindValue(":radius", 1000, PDO::PARAM_INT); 
$stm->bindParam(":latitude", $lat, PDO::PARAM_INT); 
$stm->bindParam(":longitude", $lng, PDO::PARAM_INT); 
$stm->bindParam(":start", $start, PDO::PARAM_INT); 
$stm->bindParam(":limit", $limit, PDO::PARAM_INT); 
$stm->execute(); 

下面有我的表

CREATE TABLE `location_geo` (
    `locID` int(11) unsigned NOT NULL, 
    `latitude` double DEFAULT NULL, 
    `longitude` double DEFAULT NULL, 
    PRIMARY KEY (`locID`) 
) 

伪数据执行

INSERT INTO `location_geo` (`locID`, `latitude`, `longitude`) 
VALUES 
    (1, 53.21333, -2.4354014), 
    (2, 53.213435, -2.4345214); 

我试图更换bindParams和将数据放入一个数组中执行呼叫内然而结果是相同的。

如果您错过了,纬度和经度的值将保持为空。没有来自PDO的错误。

奇怪。

回答

1

为了调试目的,我使用了setAttribute()。像这样的例子:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

这样它会告诉你,如果你犯了什么错误,它们是什么。

+0

不幸的是这不会产生任何东西在屏幕上或在日志中。谢谢无论如何。 – 2012-04-21 23:07:05

+0

尝试将其设置为一个级别highr:PDO :: ERRMODE_EXCEPTION – Mads 2012-04-21 23:09:10

1

好吧,我从来没有使用过原料PDO,但我认为错误是在这里:

$stm->bindParam(":latitude", $lat, PDO::PARAM_INT); 
$stm->bindParam(":longitude", $lng, PDO::PARAM_INT); 

你为什么说这是一个INT(我相信这PDO::PARAM_INT说什么类型的值是通过),如果你通过FLOAT?可能这就是它被转换为NULL的原因,因为浮点值不是整数...

+0

没有浮动选项,甚至设置为PARAM_STR返回相同的行为。 int选项不会像str选项那样将值包含在单引号中。然而,正如你指出这可能是问题所在,我会对此进行调查。谢谢 – 2012-04-21 21:41:58

+0

有一些关于这个问题的话题你应该看看:http://stackoverflow.com/questions/1335081/what-is-the-best-way-to-bind-decimal-double-float-values-与-pdo-in-php ..and也许你可以跳过PDO :: PARAM_ *参数,然后用'$ stm-> bindParam(“:latitude”,$ lat);'例如。 – 2012-04-21 21:47:41

+0

我已经尝试过,没有定义该文章中的建议类型,您可以在查询日志中看到mysql,它将值包装在单引号中,与PARAM_STR相同。经度和纬度的值保持为NULL。我看不到这是原因:( – 2012-04-21 22:00:40