dplase3140 2017-04-21 17:08
浏览 35

为什么单引号会阻止准备好的查询中的参数替换?

I spent some time chasing a bug in my code, and I found the issue. I was using prepared statements and using an array to replace the parameters. My issue was that by enclosing the named parameter in the prepared query, it prevent the parameter from being replaced.

Here is some sample code:

try {                                                                                                                                                                                                   
    $conn = new PDO("mysql:host=$host;dbname=$db;", $user, $pass);                                                                                                                                      
} catch(PDOException $e) {                                                                                                                                                                              
    echo $e->getMessage();                                                                                                                                                                              
}                                                                                                                                                                                                       

function insert_update_db($query, $params = NULL) {                                                                                                                                                     
global $conn;                                                                                                                                                                                           
    try {                                                                                                                                                                                               
        $sql = $conn->prepare($query);                                                                                                                                                                  
        $sql->execute($params);                                                                                                                                                                         
    }                                                                                                                                                                                                   
    catch(PDOException $e) {                                                                                                                                                                            
        echo $e->getMessage();                                                                                                                                                                          
    }                                                                                                                                                                                                   
}                                                                                                                                                                                                       
$params = array(":a" => 1, ":b" => "test_string");                                                                                                                                                      
$query = "INSERT INTO table1 VALUES (:a, :b)";                                                                                                                                                          
insert_update_db($query, $params);                                                                                                                                                            
$query = "INSERT INTO table1 VALUES (':a', ':b')";                                                                                                                                                      
insert_update_db($query, $params); 

From the general log:

Query INSERT INTO table1 VALUES ('1', 'test_string')
Query INSERT INTO table1 VALUES (':a', ':b')

As you can see from the general log, the second query does not replace the parameters. Why does that happen?

  • 写回答

1条回答 默认 最新

  • dongshou2017 2017-04-21 17:31
    关注

    The prepare method is analyzing the SQL query string looking for the replacements (:var or ?). The quoted values are not replaced, otherwise you wouldn't be able to store a literal ':b' value in a table field. Here you have further information about PDO::prepare http://php.net/manual/en/pdo.prepare.php

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化