PHP PDO 在函数中执行
我已经创建了这个类:
class data
{
public function del($cat, $id)
{
global $dbh;
$del = $dbh->prepare("DELETE FROM :cat WHERE id = :id");
$del->bindParam(":cat",$cat);
$del->bindParam(":id", $id);
$del->execute();
}
}
并且我遇到了将
:cat
变量绑定到语句的问题,如果我不使用 bindParam for
:cat
并只是告诉它我希望它从哪个表中删除它,例如:
$del = $dbh->prepare("DELETE FROM table1 WHERE id = :id");
它有效美好的。
我知道这一定是一些愚蠢的错误,但我一生都无法弄清楚。
I've created this class:
class data
{
public function del($cat, $id)
{
global $dbh;
$del = $dbh->prepare("DELETE FROM :cat WHERE id = :id");
$del->bindParam(":cat",$cat);
$del->bindParam(":id", $id);
$del->execute();
}
}
And I'm running into an issue with binding the
:cat
variable to the statement, if I don't use bindParam for
:cat
and just tell it which table I want it to delete it from, for example:
$del = $dbh->prepare("DELETE FROM table1 WHERE id = :id");
It works fine.
I know it has to be some stupid error, but I can't for the life of me figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来你想要做的根本不是可能的
It seems what you're trying to do is simply not possible
PDO 无法使用表作为参数名称。
堆栈溢出帖子
php.net 帖子
Using table as parameter name is not possible with PDO.
Stack overflow post
php.net post