简体   繁体   中英

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. In SQL, "DELETE FROM" removes records from the specified database. It is more common to see 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.

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.

If you want to remove the table (not just delete the data contained therein), use DROP TABLE .

If you want to DELETE all rows in a table, you should use 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. TRUNCATE will empty your table almost instantly

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. And THINK before executing this, it is a dangerous script if used on the wrong database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM