如何在MySQL中使用预处理语句截断表? [英] How to truncate a table using prepared statement in MySQL?

查看:181
本文介绍了如何在MySQL中使用预处理语句截断表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将返回true,但并未截断该表:

This returns true but it didn't truncate the table:

$this->db->query("TRUNCATE TABLE $tablename");

但是它可以在为准备好的语句创建数据库连接对象之前起作用.

But it works before creating a database connection object for prepared statement.

如何解决?另外,我想知道如何使用准备好的语句截断表.

How to fix it? Also, I want to know how to truncate the table using prepared statement.

推荐答案

,准备好的语句不是解决方案,因为无法绑定表名. 因此,请避免在截断表中使用准备好的语句.

NO, A prepared statement would not be a solution because it is not possible to bind the table name. So avoid to use prepared statement for Truncate Table.

您不能绑定任何SQL文字,但只能绑定一个数据.因此,不能使用准备好的语句来绑定关键字,运算符和任何标识符.您只能绑定数据.

You cannot bind any SQL literal but data one. So keywords, operators and any identifier can not be bind using prepared statement. You can only bind data.

PDO准备好的语句在运行带有用户输入的查询时非常有用,因为它们允许您使用绑定参数等功能来清理用户输入.

PDO prepared statements are useful when running queries with user input as they allow you to use features such as bound parameters to sanitise user input.

因此,在我的建议下,您不应将准备好的语句用于截断表.

So In my suggestion you should not use prepared statement for truncate table.

如果您真的想使用prepare进行截断,如果使用的是Opencart,请使用以下代码:

If you really want to truncate using prepared , In case of Opencart which you are using, Use the code:

$sql = sprintf('TRUNCATE TABLE %s%s', DB_PREFIX, $table);
$this->db->query($sql); 

尝试一次,让我知道

这篇关于如何在MySQL中使用预处理语句截断表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆