[英]delete a data from database from multiple tables
<?php
//after creating connection
$dbname = 'bca2y';//database name
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn , $sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysqli_fetch_row($result)) {
$table = "{$row[0]}\n";
echo "$table<br>";
if($conn == TRUE){
echo "connection is possible<br>";
}
$sqld = "DELETE FROM $table";
$resultd = $conn->query($sqld);
if($resultd === TRUE){
echo "Data deleted succesfully ";//checking if data deleted
}
else {
echo "some error<br>";// for checking if code is not running
}
}
?>
Here I am trying to find name of table and delete data from the given table But I think there is any syntax error in using variable as a table name. 在这里,我试图查找表的名称并从给定的表中删除数据,但是我认为使用变量作为表名存在任何语法错误。 my code has not giving any error but it still not working. 我的代码没有给出任何错误,但仍然无法正常工作。
If I am understanding your intention -- to find table(s) and delete them from the database, "DELETE FROM mes" does not accomplish this goal. 如果我了解您的意图-查找表并从数据库中删除它们,则“ DELETE FROM mes”不会实现此目标。 In SQL, "DELETE FROM" removes records from the specified database. 在SQL中,“ DELETE FROM”从指定的数据库中删除记录 。 It is more common to see DELETE FROM TableName WHERE ColumnName is 'SomeValue';
更常见的是,在DELETE FROM TableName WHERE ColumnName is 'SomeValue';
-- a deletion of some set of records. -删除某些记录集 。 But DELETE FROM TableName
is a valid SQL query -- it deletes all records from the table named TableName. 但是DELETE FROM TableName
是有效的SQL查询-它从名为TableName的表中删除所有记录。
If you wish to delete all records from the table names retrieved from your first query, you would need to use a variable name in your delete statement rather than the static string mes. 如果要删除从第一个查询检索到的表名中的所有记录,则需要在delete语句中使用变量名,而不是静态字符串mes。
If you want to remove the table (not just delete the data contained therein), use DROP TABLE . 如果要删除表(而不仅仅是删除表中包含的数据),请使用DROP TABLE 。
If you want to DELETE all rows in a table, you should use TRUNCATE
. 如果要删除表中的所有行 ,则应使用TRUNCATE
。
DELETE
will delete row per row, which might take a lot of time because a lot of stuff is done by mysql for each delete operation. DELETE
将删除每一行,这可能会花费很多时间,因为mysql会为每个删除操作完成很多工作。 TRUNCATE
will empty your table almost instantly TRUNCATE
几乎立即清空您的桌子
Here's your script adapted to achieve what you want 这是您的脚本,可满足您的需求
$dbname = 'bca2y';//database name
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE '".$dbname."'";
$result = mysqli_query($conn , $sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach($rows as $table)
{
$sql = "TRUNCATE TABLE `".$table['TABLE_NAME']."`";
$resultd = $conn->query($sql);
if($resultd === TRUE){
echo $table['TABLE_NAME']. " truncated succesfully ";//checking if data deleted
}
else {
echo "some error<br>";// for checking if code is not running
}
}
Your user should have TRUNCATE permissions of course. 您的用户当然应该具有TRUNCATE权限。 And THINK before executing this, it is a dangerous script if used on the wrong database. 在执行此操作之前,请考虑一下,如果在错误的数据库上使用它,这将是一个危险的脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.