通过 PHP 函数截断 MySQL table 函数不起作用
Truncate MySQL table via PHP Function not working
我一直在四处寻找,发现其他人也有类似的问题,但在大多数情况下,他们只是在调用他们的数据库连接之前没有使用 'global' 关键字。基本上,我正在尝试 运行 一个函数,该函数 t运行 根据函数中传递的内容分类 table。
在recipes.php
truncate_table('bl_recipes');
并在 functions.php
// Truncate table passed in function
function truncate_table($table) {
global $con;
$truncate_query = '"TRUNCATE TABLE ' . $table . '"';
$truncate = mysqli_query($con, $truncate_query);
if ($truncate) {
echo 'Table truncated.';
}
else {
echo 'Table not truncated.';
echo 'The query was ' . $truncate_query;
}
}
如果我 echo $t运行cate 我得到 TRUNCATE TABLE 'bl_recipes';如果我从 PHPMyAdmin 手动 运行cate,这正是我得到的。 $con 中的详细信息与其他查询一样正确,尽管其中 none 来自 functions.php 文件。数据库连接本身目前在 functions.php 中定义,稍后我可能会移动它,但目前在开发过程中我并不关心它在哪里。用户拥有所有数据库权限。页面加载结果为 "Table not truncated".
有人知道我做错了什么吗?
干杯,
李。
使用这个
$truncate_query = 'TRUNCATE TABLE `' . $table .'`';
或这个
$truncate_query = "TRUNCATE TABLE `$table`";
我一直在四处寻找,发现其他人也有类似的问题,但在大多数情况下,他们只是在调用他们的数据库连接之前没有使用 'global' 关键字。基本上,我正在尝试 运行 一个函数,该函数 t运行 根据函数中传递的内容分类 table。
在recipes.php
truncate_table('bl_recipes');
并在 functions.php
// Truncate table passed in function
function truncate_table($table) {
global $con;
$truncate_query = '"TRUNCATE TABLE ' . $table . '"';
$truncate = mysqli_query($con, $truncate_query);
if ($truncate) {
echo 'Table truncated.';
}
else {
echo 'Table not truncated.';
echo 'The query was ' . $truncate_query;
}
}
如果我 echo $t运行cate 我得到 TRUNCATE TABLE 'bl_recipes';如果我从 PHPMyAdmin 手动 运行cate,这正是我得到的。 $con 中的详细信息与其他查询一样正确,尽管其中 none 来自 functions.php 文件。数据库连接本身目前在 functions.php 中定义,稍后我可能会移动它,但目前在开发过程中我并不关心它在哪里。用户拥有所有数据库权限。页面加载结果为 "Table not truncated".
有人知道我做错了什么吗? 干杯, 李。
使用这个
$truncate_query = 'TRUNCATE TABLE `' . $table .'`';
或这个
$truncate_query = "TRUNCATE TABLE `$table`";