【问题标题】:custom mysqli class has an error with php 5.4?自定义 mysqli 类与 php 5.4 有错误?
【发布时间】:2012-05-03 18:18:04
【问题描述】:

我刚刚将我的网络服务器升级到 php 5.4,并且在使用从内置 mysqli 扩展的数据库类的网站上遇到错误。错误出现在我班级的最后一行,尽管出现错误消息,但一切正常....

错误信息:

Strict Standards: Declaration of yamiko_mysqli::connect() should be compatible with mysqli::connect($host = NULL, $user = NULL, $password = NULL, $database = NULL, $port = NULL, $socket = NULL) in /home/markwe6/public_html/_php/yamiko_mysqli.php on line 109

班级是:

class Yamiko_mysqli extends mysqli
{
    public $host='localhost';
    public $user='markwe6_yamiko';
    public $pass='1chrysanthemum!';
    public $db='markwe6_cp';
    public $result=NULL;#stores most recent result

    /*
     * 
     */
    public function __construct($auto=TRUE)
    {
        if($auto)
        {
            return $this->connect();
        }else
        {
            return TRUE;
        }
    }

    /*
     * 
     */
    public function connect($auto=TRUE, $user=NULL, $pass=NULL, $host=NULL, $db=NULL)
    {
        if($auto)
        {
            parent::__construct($this->host, $this->user, $this->pass, $this->db);
            return $this->check_error();
        }else
        {
            parent::__construct($host, $user, $pass, $db);
            return $this->check_error();
        }
    }

    /*
     * 
     */
    public function query($sql)
    {
        $result=parent::query($sql);
        if($this->check_error())
            return FALSE;
        $this->result=$result;
        return $result;
    }

    /*
     * 
     */
    private function check_error()
    {
        if($this->connect_error!=NULL)
        {
            $GLOBALS['yamiko']->set_error('yamiko_myslqi connection error: '.$this->connect_error);
            return FALSE;
        }elseif ($this->error!=NULL)
        {
            $GLOBALS['yamiko']->set_error('yamiko_myslqi error: '.$this->error);
            return FALSE;
        }
    }
}#this is line 109....-_-

【问题讨论】:

  • 错误信息的哪一部分你不明白? -or- 你的问题是什么?
  • 我完全不明白这个错误......课程工作得很好

标签: mysqli php strict


【解决方案1】:

自定义 mysqli 类在 php 5.4 中出现错误?

不,不是错误,而是严格的标准警告。如果您认为警告是错误,那么是的,您的自定义 mysqli 类在 php 5.4 中存在错误。

严格标准的警告内容如下:

如果您打算从基类扩展。连接函数的声明必须与基类之一匹配:

mysqli::connect($host = NULL, $user = NULL, $password = NULL, $database = NULL, $port = NULL, $socket = NULL)

在你的情况下它没有:

Yamiko_mysqli::connect($auto=TRUE, $user=NULL, $pass=NULL, $host=NULL, $db=NULL)

如您所见,两者都有不同的参数。

在您的情况下,修复相当简单,您只需重新使用第一个参数,如果 NULL 您提供类自己的默认值:

/*
 * 
 */
public function connect($host = NULL, $user = NULL, $password = NULL, $database = NULL, $port = NULL, $socket = NULL)
{
    if($host === NULL)
    {
        parent::__construct($this->host, $this->user, $this->pass, $this->db);
        return $this->check_error();
    }else
    {
        parent::__construct($host, $user, $password , $database, $port, $socket);
        return $this->check_error();
    }
}

请注意默认配置中缺少端口和套接字。

【讨论】:

  • 从显示中?可能禁用display_errors。否则,您应该使用相同的参数,或者不要从mysqli 扩展。我也更新了答案,因此它包含更多信息。
  • @yamikoWebs:刚刚添加了一个简单的修复,看看你需要什么。
  • 是的,谢谢。很抱歉延迟接受答案,我已经有几天没有参加了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2017-07-14
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多