brietsparks
brietsparks

Reputation: 5016

prepared statement values not binding

I am trying to bind parameters in a prepared sql statement, and am not able to do so. The code I have is:

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

The parameters don't seem to be binding. I am getting a statement returned: PDOStatement Object ( [queryString] => SELECT :target from accounts WHERE :index = :index_value ) How do I fix this?

Upvotes: 1

Views: 131

Answers (1)

meda
meda

Reputation: 45490

Binding columns is not going to work you're going to have to build the string directly:

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

Upvotes: 1

Related Questions