开发者

PDO bindValues isn't binding my values

开发者 https://www.devze.com 2023-01-17 08:59 出处:网络
I can\'t get bindValue to bind my values to the sql query. $sql = \"INSERT INTO :table(:columns) VALUES(:values)\";

I can't get bindValue to bind my values to the sql query.

 $sql = "INSERT INTO :table(:columns) VALUES(:values)";
 $query = $conn->prepare($sql);
 $query->bindValue(':table',$table);
 $query->bindValue(':columns',$columns);
 $query->bindValue(':values',$values);
 $query->execute();

When I run this, $query->execute() returns "false" and the data isn't update to the DB. I've also tried:

 $sql = "INSERT INTO :table(:columns) VALUES(:values)";
 $query = $conn->prepare($sql);
 $query->execute(array('table'=>$table,':columns'=>$columns,':values'=>$values));

and it still doesn't work.

This works but isn't what I want to do:

 $sql = "INSERT INTO $table($columns) VALUES($values)";
 $result = 开发者_如何学Python$conn->query($sql);

Please tell me what I'm doing wrong. Thanks.


You are using it incorrectly, you cannot dynamically assign structural SQL values etc via the bindParam as it is meant for column values being inserted in / updated / tested against.

UPDATE

If you provide us with what the $columns and $variables (as Col. Shrapnel asked in the comments) contents generally are / where they come from, I / we maybe able to help you with a work around to your predicament.

0

精彩评论

暂无评论...
验证码 换一张
取 消