dongshuobei1037 2014-04-02 20:26
浏览 64
已采纳

将MYSQL *查询转换为带占位符的PDO ready语句

Im having real trouble converting a SQL query from mysql to a PDO prepared statement. It is the following and it is used to calculate the distance between a coordinate stored in a session (the users position) and a coordinate in the database, and if it is within a radius determined by the user:

$query = "
SELECT 
* 
FROM 
first_page_data 
WHERE 
((((acos(sin((".$_SESSION['alat']."*pi()/180)) * 
sin(('geo_lat1'*pi()/180))+cos((".$_SESSION['alat']."*pi()/180)) * 
cos(('geo_lat1'*pi()/180)) * cos(((".$_SESSION['alon']."- 'geo_lon1') * 
pi()/180))))*180/pi())*60*1.1515) * (1.609344/1000)) < ".$_SESSION['aradius'];

I want to change the session variables, and the geo_lat and geo_lon variables into placeholders so I can bind the values to them but I cant for the life of me get it to work!

I dont even know how to see how im getting on because once ive replaced the variables above with placeholders (ive been using the unnamed questionmark placeholders) and then binded the values to them, I dont know how to retrieve the compiled query from $stmt before I execute it:

$stmt = db->prepare($query);
$stmt->bindValue(1, $_SESSION['alat'], PDO::PARAM_STR);
**more bindings**
$stmt->execute();   
  • 写回答

1条回答 默认 最新

  • dtwvr26066 2014-04-02 20:39
    关注

    These changes are very trivial. I put in placeholder names, removed the string concatenation, and removed the incorrect quotes. It could use help with formatting, or perhaps a UDF.

    # Change string-concat for placeholders
    $query = "
    SELECT 
    * 
    FROM 
    first_page_data 
    WHERE 
    ((((acos(sin(( :lat1 *pi()/180)) * 
    sin((geo_lat1*pi()/180))+cos(( :lat2 *pi()/180)) * 
    cos((geo_lat1*pi()/180)) * cos((( :lon - geo_lon1) * 
    pi()/180))))*180/pi())*60*1.1515) * (1.609344/1000)) < :rad";
    
    # Bind values (PARAM_STR is default)
    # While PDO does support using a named parameter multiple times
    # when emulating placeholders, this is not guaranteed. To be safe,
    # here the code is binding the same value to two "different" params.
    $stmt = db->prepare($query);    
    $stmt->bindValue(":lat1", $_SESSION['alat']);
    $stmt->bindValue(":lat2", $_SESSION['alat']);
    $stmt->bindValue(":lon", $_SESSION['along']);
    $stmt->bindValue(":rad", $_SESSION['aradius']);
    
    # Execute! I recommend enabling PDO Exceptions to avoid so much
    # manual error-checking of result values.
    $stmt->execute();   
    

    Now, there is no direct way to see the statement with data as that's not how placeholders work - even if PDO internally emulates placeholders it's merely an implementation detail and you don't get to see it.

    However, there are various solutions to the more general task of "debugging statements" discussed in How to debug PDO database queries? (I recommend using the database logging for development as your code probably shouldn't be logging or printing queries directly.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?