PHP & MySQL: Truncate multiple tables

前端 未结 3 1477
醉梦人生
醉梦人生 2020-12-20 02:02

I tried to truncate a table but why is it not working? must something wrong in the database query?

$sql = \"TRUNCATE TABLE `table_name`\";

$result = $connec         


        
相关标签:
3条回答
  • 2020-12-20 02:42

    By the MySQL Reference Manual

    http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html

    You can only delete one table at a time using TRUNCATE.

    You could try executing multiple queries in one PHP query by using a ";" delimiter between them.

    0 讨论(0)
  • 2020-12-20 02:45

    thanks for the help guys! here is my answer,

    # truncate data from all table
    # $sql = "SHOW TABLES IN 1hundred_2011";
    # or,
    $sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE '".DB_NAME."'";
    
    # use the instantiated db connection object from the init.php, to process the query
    $tables = $connection -> fetch_all($sql);
    //print_r($tables);
    
    foreach($tables as $table) 
    {
        //echo $table['TABLE_NAME'].'<br/>';
    
        # truncate data from this table
        # $sql = "TRUNCATE TABLE `developer_configurations_cms`";
    
        # use the instantiated db connection object from the init.php, to process the query
        # $result = $connection -> query($sql);
    
        # truncate data from this table
        $sql = "TRUNCATE TABLE `".$table['TABLE_NAME']."`";
    
        # use the instantiated db connection object from the init.php, to process the query
        $result = $connection -> query($sql);
    }
    
    0 讨论(0)
  • 2020-12-20 02:55

    You have a couple different option.

    • Run a query for each truncate
    • Create a stored procedure that will truncate all the tables you want to do.
    0 讨论(0)
提交回复
热议问题