PHP& MySQL:截断多个表 [英] PHP & MySQL: Truncate multiple tables
本文介绍了PHP& MySQL:截断多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图截断一个表,但是为什么它不起作用?数据库查询中一定有问题吗?
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 = $connection -> query($sql);
理想情况下,我想一次性截断所有表-有可能吗?
Ideally, I want to truncate all tables in one go - is it possible?
如果您想知道我用来进行数据库查询的类的内容是什么,
if you wonder what is inside the class that I use to make the database queries, here is it,
#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对象
below is how I call the db object,
# 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);
推荐答案
感谢帮助人员!这是我的答案,
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);
}
这篇关于PHP& MySQL:截断多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文