English 中文(简体)
PHP PDO 约束Param 返回NUL
原标题:PHP PDO bindParam returning NULL
  • 时间:2012-04-21 21:23:45
  •  标签:
  • php
  • mysql
  • pdo

我有一个与项目设计书具有约束力的参数有关的问题,所设定的参数在数据库上执行时没有被推入询问。 我亲眼目睹了这一情况,把问询记录到档案中。

如果我对服务器投射,我就看到这一点。

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();

Heres my table

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

Dummy数据 ——

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

我已尝试将捆包巴拉姆替换成一个阵列,并在执行呼吁中将数据放在一个阵列中,但结果相同。

如果你错失,纬度和长度的数值仍然无效。 PDO没有错误。

Weird。

最佳回答

为减少偷窃目的,我使用 set(Attribute)。 与此类似:

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

这样,你就会告诉你,你会犯错误和错误。

问题回答

奥基以前从未使用过原始设计书,但我认为这里的错误是:

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

为什么要说是“国际”组织(我假定这一条码)。 PDO:PARAM_INT/code> 说,如果你通过FLOAT,价值究竟是怎样的? 值得注意的是,由于浮动点值不是惯性,因此它正被转换为NUL。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签