PHP 5.2 上的 'SQLSTATE[42000] 未捕获异常 'PDOException' 但 PHP 5.4 中没有

Uncaught exception 'PDOException' with 'SQLSTATE[42000] on PHP 5.2 but not in PHP 5.4

为什么 运行 下面的语法在 PHP 5.4 而不是 PHP 5.2?

$stmt = $this->pdo->prepare('SELECT *, COALESCE(( 6371 * acos( cos( radians(:lat_params) ) *  
cos( radians( tbl_restaurants_restaurants.lat ) ) *                                    
cos( radians( tbl_restaurants_restaurants.lon ) - radians(:lon_params) ) + 
sin( radians(:lat_params1) ) * 
sin( radians( tbl_restaurants_restaurants.lat ) ) ) ), 0) AS 
distance FROM 
tbl_restaurants_restaurants WHERE tbl_restaurants_restaurants.is_deleted = 0 
ORDER BY distance ASC LIMIT 0, :max');

$stmt->execute( array('lat_params' => $lat, 'lon_params' => $lon, 'lat_params1' => $lat, 'max' => $max ));
    return $stmt;

我有一个错误,这应该是由于 PHP 5.2 兼容性。在 PHP 5.4 中此脚本正常工作。

link 重现了同样的错误。

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''20'' at line 10' in D:...\ControllerRest.php:56 Stack trace: #0 D:...\ControllerRest.php(56): PDOStatement->execute(Array) #1 D:...\get_data.php(42): ControllerRest->getRestaurantsNearbyResultsAtCount('-20.290190', '-40.293366', '20') #2 {main} thrown in D:...\ControllerRest.php on line 56

在 PHP 5.2 上 运行 是否有此脚本的替代方案?

==更新==

shivanshu patel 的回答有效!但现在该错误是由以下 SQL 语句生成的:

$stmt = $this->pdo->prepare("SELECT COALESCE(( 6371 * acos( cos( radians(:lat_params) ) * cos( radians( 'tbl_restaurants_restaurants'.'lat' ) ) * cos( radians( 'tbl_restaurants_restaurants'.'lon' ) - radians(:lon_params) ) + sin( radians(:lat_params1) ) * sin( radians( 'tbl_restaurants_restaurants'.'lat' ) ) ) ), 0) AS distance FROM 'tbl_restaurants_restaurants' ORDER BY distance DESC LIMIT 0, :default_to_find_distance");

$stmt->execute( array('lat_params' => $lat, 'lon_params' => $lon, 'lat_params1' => $lat, 'default_to_find_distance' => $default_to_find_distance) );

尝试用 `` 覆盖列和表,并确保最大值为整数。

<?php
$stmt = $this->pdo->prepare('SELECT *, COALESCE(( 6371 * acos( cos( radians(:lat_params) ) *
cos( radians( `tbl_restaurants_restaurants`.`lat` ) ) *
cos( radians( `tbl_restaurants_restaurants`.`lon` ) - radians(:lon_params) ) +
sin( radians(:lat_params1) ) *
sin( radians( `tbl_restaurants_restaurants`.`lat` ) ) ) ), 0) AS
distance FROM
`tbl_restaurants_restaurants` WHERE `tbl_restaurants_restaurants`.`is_deleted` = 0
ORDER BY distance ASC LIMIT 0, :max');

$stmt->bindValue(':max', $max, PDO::PARAM_INT);
$stmt->execute( array('lat_params' => $lat, 'lon_params' => $lon, 'lat_params1' => $lat ));
return $stmt;