0
下面的错误类函数是不完整/不正确的吗?我永远不能得到任何错误消息时查询不正确,PHP:类显示错误函数
#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()函数,但仍然没有任何东西已显示从错误函数...
+1更清洁 – HyderA
感谢。当我测试你的代码时出现这个错误 - 致命错误:调用未定义的方法mysqli :: fetch_all()在C:\ wamp \ www \ ... \ class_database.php xxx行 - 任何想法? – laukok
已更新。但我的代码纯粹仅用于示例目的。 – RobertPitt