duandang6352 2017-06-21 14:25
浏览 24
已采纳

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

Why does the syntax below run in PHP 5.4 but not in 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;

I am having an error which is supposed due to the PHP 5.2 compatibility. In PHP 5.4 this script works normally.

This link reproduces the same error.

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

Is there any alternative to this script that runs on PHP 5.2?

==UPDATE==

shivanshu patel's answer did works! But now that error is generate by the following SQL statement:

$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条回答 默认 最新

  • doulun1666 2017-06-22 14:51
    关注

    Try to cover columns and tables with `` and make sure max is in 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理