【发布时间】:2011-02-20 16:11:43
【问题描述】:
下面的错误类函数中是否存在不完整/不正确的内容?当查询不正确时,我永远无法从中得到任何错误消息,
#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();
}
}
#fetches all result rows as an associative array, a numeric array, or both
public function fetch_all($query)
{
$result = $this -> connection -> query($query);
if($result)
{
return $result -> fetch_all(MYSQLI_ASSOC);
}
else
{
$this -> error = $this -> connection -> error;
return $this -> error;
}
}
#display error
public function get_error()
{
return $this -> error;
}
#closes the database connection when object is destroyed.
public function __destruct()
{
$this -> connection -> close();
}
}
public function get_error() 在我的 db 类中似乎没用...我已经阅读了有关 php Exception 的信息,但我不知道如何将它合并到上面的这个 db 类中!请指教...
编辑:
我试着把代码改成这个,
# return the current row of a result set as an object
public function fetch_object($query)
{
$result = $this->connection->query($query);
if($result)
{
...
}
else
{
__database::get_error();
}
}
和错误类函数,
#display error
public function get_error()
{
$this->error = $this->connection->error;
return $this->error;
}
所以我认为它应该触发 get_error() 函数,但是错误函数仍然没有显示任何内容...
【问题讨论】:
标签: php oop exception exception-handling error-handling