【问题标题】:PHP: class display error functionPHP:类显示错误函数
【发布时间】: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


    【解决方案1】:

    首先,如果您想充分利用 mysqli 类,那么您应该扩展它,并在需要的地方覆盖。

    class Database extends mysqli
    {
        public function __construct($host = null,$username= null,$password = null,$database = "",$port = null, $socket = null)
        {
             $host     = $host     !== null ? $host     : ini_get("mysqli.default_host");
             $username = $username !== null ? $username : ini_get("mysqli.default_user");
             $password = $password !== null ? $password : ini_get("mysqli.default_pw");
             $port     = $port     !== null ? $port     : ini_get("mysqli.default_port");
             $socket   = $socket   !== null ? $socket   : ini_get("mysqli.default_socket");
    
             /*
                 Perform any custom actions here!
             */
    
             parent::__construct($host,$username,$password,$database,$port,$socket);
        }
    
        public function fetch_all($query) /*Overridden*/
        {
            if(false !== ($result = parent::query($query))) //run directly in mysqli
            {
                 return $result->fetch_all(MYSQLI_ASSOC);
            }
    
            return false;
        }
    
        public function get_error() 
        {
            if($this->errno || $this->error)
            {
                return sprintf("Error (%d): %s",$this->errno,$this->error);
            }
        }
    }
    

    这将允许 mysqli 处理错误并允许您轻松访问它们,创建一个仅仅模仿一个类的类没有意义,您最好扩展类本身。

    其次,将代码隔开是一种不好的做法,它不应该影响 php 解释代码的方式,但它可能会使进一步的开发人员感到困惑,并从长远来看,当涉及到共享开发时会导致问题。

    简单示例:

    $Database = new Database(null,"root","pass","database");
    $Results = $Database->fetch_all("SELECT * from tabe_that_dont_exists");
    if($Results === false)
    {
         echo $Database->get_error();
    }
    

    【讨论】:

    • 谢谢。我在测试您的代码时收到此错误 - 致命错误:在第 xxx 行的 C:\wamp\www\...\class_database.php 中调用未定义的方法 mysqli::fetch_all() - 有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多