【问题标题】:PHP & MySQL: Truncate multiple tablesPHP & MySQL:截断多个表
【发布时间】:2011-06-09 19:36:59
【问题描述】:

我试图截断一个表格,但为什么它不起作用?数据库查询一定有问题吗?

$sql = "TRUNCATE TABLE `table_name`";

$result = $connection -> query($sql);

理想情况下,我想一次性截断所有表 - 可以吗?

如果你想知道我用来进行数据库查询的类里面有什么,这里就是,

#connects the database and handling the result
class __database {

 protected $connection = null;
 protected $error = null;

 #make a connection
 public function __construct($hostname,$username,$password,$database)
 {
  $this -> connection = new mysqli($hostname,$username,$password,$database);

  if (mysqli_connect_errno()) 
  {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
  }
 }

 ...

 #performs a query on the database
 public function query($query)
 {
  $result = $this -> connection -> query($query); 
  if($result) 
  {
   return $result;
  } 
  else
  {
   $this -> error = $this -> connection -> error;
   return false;
  }

 }


 #display error
 public function get_error() 
 {
  return $this -> error;
 }

 #closes the database connection when object is destroyed.
    public function __destruct()
    {
        $this -> connection -> close();
    }
}

谢谢。

编辑:

下面是我如何调用 db 对象,

# the host used to access DB
define('DB_HOST', 'localhost');

# the username used to access DB
define('DB_USER', 'root');

# the password for the username
define('DB_PASS', 'xxx');

# the name of your databse 
define('DB_NAME', 'xxx'); 

$connection = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);

【问题讨论】:

  • 什么都没有/根本没有显示错误!我不知道为什么我做的db类在一定有错误的地方不能显示错误!
  • 您是否尝试过调用 get_error() 方法?您能否提供更多创建数据库对象/调用它的代码?
  • @jasonbar:我在编辑中提供了上面的代码。问题是我从不调用 get_error() 方法,因为我不知道如何调用它!从其他地方的教程中制作了那个 db 类,现在我已经忘记了一切!
  • 仔细检查mysql用户是否有足够的权限。查看mysql错误日志。确保您没有混淆开发和实时数据库(截断一个,验证另一个)。
  • 发现错误。我运行了错误的文件! arggggg - 愚蠢的我!!!感谢您的帮助!

标签: php mysql sql truncate


【解决方案1】:

根据 MySQL 参考手册

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

使用 TRUNCATE 一次只能删除一个表。

您可以尝试使用“;”在一个 PHP 查询中执行多个查询它们之间的分隔符。

【讨论】:

    【解决方案2】:

    感谢大家的帮助!这是我的答案,

    # 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);
    }
    

    【讨论】:

    【解决方案3】:

    你有几个不同的选择。

    • 为每个截断运行查询
    • 创建一个存储过程,该过程将截断您想要执行的所有表。

    【讨论】:

    • 由于某些原因,我什至无法截断表格。一定是数据库查询中的错误吗?
    猜你喜欢
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2014-12-01
    相关资源
    最近更新 更多