PDO bindValues 没有绑定我的值
我无法让bindValue 将我的值绑定到sql 查询。
$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();
当我运行此命令时, $query->execute() 返回“false”并且数据不会更新到数据库。我也试过了:
$sql = "INSERT INTO :table(:columns) VALUES(:values)";
$query = $conn->prepare($sql);
$query->execute(array('table'=>$table,':columns'=>$columns,':values'=>$values));
还是不行。
这可行,但不是我想要做的:
$sql = "INSERT INTO $table($columns) VALUES($values)";
$result = $conn->query($sql);
请告诉我我做错了什么。谢谢。
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 = $conn->query($sql);
Please tell me what I'm doing wrong. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用不正确,您无法通过bindParam动态分配结构SQL值等,因为它意味着插入/更新/测试的列值。
更新
如果您向我们提供
$columns
和$variables
(正如Shrapnel上校在评论中询问的那样)内容通常是什么/它们在哪里来自,我/我们也许可以帮助您解决您的困境。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.