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

时间:2017-06-21 14:25:23

标签: php php-5.4 php-5.2

为什么下面的语法在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会再现相同的错误。

  

致命错误:未捕获的异常' PDOException'与消息   ' SQLSTATE [42000]:       语法错误或访问冲突:1064 SQL语法中有错误;       查看与您的MySQL服务器版本对应的手册,以获得在' 20''''''''''       在第10行'在D:... \ ControllerRest.php:56       堆栈跟踪:#0 D:... \ ControllerRest.php(56):       PDOStatement-> execute(Array)#1 D:... \ get_data.php(42):       ControllerRest-> getRestaurantsNearbyResultsAtCount(' -20.290190',' -40.293366',' 20')       #2 {main}抛出D:...... \ ControllerRest.php第56行

在PHP 5.2上运行此脚本是否有其他替代方法?

== UPDATE ==

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

1 个答案:

答案 0 :(得分:1)

尝试用``覆盖列和表,并确保max在int。

<?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;