【发布时间】: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 - 愚蠢的我!!!感谢您的帮助!