准备好的语句值不具有约

时间:2014-06-10 23:01:57

标签: php mysql pdo

我正在尝试在准备好的sql语句中绑定参数,但我无法这样做。我的代码是:

function accountGet($database,$target,$index,$index_value) {
    $sql = 'SELECT :target from accounts WHERE :index = :index_value';
    $query = $database->prepare($sql);
    $query->execute(array(
        ':target' => $target,
        ':index' => $index,
        ':index_value' => $index_value
    ));
    print_r($query);
}

参数似乎没有约束力。我收到一条声明:PDOStatement Object ( [queryString] => SELECT :target from accounts WHERE :index = :index_value )我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

绑定列不起作用,您将不得不直接构建字符串:

   function accountGet($database,$target,$index,$index_value) {
        $sql = "SELECT `$target` from accounts WHERE `$index` = :index_value";
        $query = $database->prepare($sql);
        $query->execute(array(':index_value' => $index_value));
        print_r($query);    
        //$result = $query->fetchAll();     
        //print_r($result);
    }