PHP扩展到MySQLI类

时间:2012-06-27 01:14:00

标签: php mysqli

我创建了一个类,我将我的类扩展到PHP中的mysqli类,我真的很震惊地看到这些错误

Warning: Missing argument 1 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

Warning: Missing argument 2 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

Warning: Missing argument 3 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

Warning: Missing argument 4 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

这是BeatBeast_Db.php

<?php
    class BeatBeast_Database extends mysqli
    {

        protected $r = 'Something';
        public function __construct($db_host,$db_username,$db_password,$db_name)
        {
            parent::__construct($db_host,$db_username,$db_password,$db_name);

            if(mysqli_connect_error())
            {
                die('Connect Error (' . mysqli_connect_errno() . ')' . mysqli_connect_error());
            }
        }

        public function close()
        {
            $this->close();
        }

    }

    require_once("db_constants.inc.php");
    $conn = new BeatBeast_Database("localhost", "root", "myPass", "beatbeast");

这是我的login.php

<?php require_once("./includes/Utilities.php") ;?>
<?php require_once("./includes/Db/DatabaseUtilities.php"); ?>
<?php require_once("./includes/Db/Accounts.php");?>
<?php require_once("./includes/Db/BeatBeast_Db.php"); ?>

<?php 
    if(isset($_POST['submit'])){
        require_once("./includes/process_form.inc.php");

        $hashedPass = crypt($password,$username);
        $accounts = new Accounts();
        $accounts->showMessage();

和第11行是

$accounts = new Accounts();

如果你们有兴趣,这是我的账户课程

require_once( “BeatBeast_Db.php”);

Class Accounts extends BeatBeast_Database
{
    private $accnt_id;
    private $username;
    private $email;

    function info()
    {

        echo "{$this->accnt_id} {$this->username} {$this->email}";
    }

    public static function getIdByUsername($username)
    {
        global $conn;
        $sql = "SELECT accnt_id FROM accounts WHERE username = '{$username}'";
        $rs = $conn->query($sql);
        $found = $rs->fetch_array();
        return $found;
    }

    public function showMessage(){
        echo "{$this->r}";
    }

    public static function getUsernameById($id)
    {
        global $conn;
            $sql = "SELECT username FROM accounts WHERE accnt_id = $id ";
        $rs = $conn->query($sql);
        $found = $rs->fetch_array();
        return $found;
    }

    public function getAccntId()
    {
        return $this->accnt_id;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getEmail()
    {
        return $this->email;
    }
}

2 个答案:

答案 0 :(得分:1)

如果查看MySQLi的PHP参考(http://www.php.net/manual/en/class.mysqli.php),原始类构造函数包含6个参数:

__construct ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

扩展MySQLi时,即使您没有使用,也必须定义所有这些内容。但是,这不是造成警告错误的原因。

在您的情况下,您在不定义任何参数的情况下调用$accounts = new Accounts();。但是,Accounts类范围BeatBeast_Database。因此,您需要传递所有BeatBeast_Database构造函数参数($db_host,$db_username,$db_password,$db_name)。

答案 1 :(得分:0)

让我们从login.php第11行开始

$accounts = new Accounts();

这会调用Accounts类的构造函数 - 它没有定义。由于

Class Accounts extends BeatBeast_Database

PHP回退到BeatBeast_Database类的构造函数:

public function __construct($db_host,$db_username,$db_password,$db_name)

需要4个参数,但没有 - 完全是错误信息中的内容。您需要将login.php的第11行更改为

$accounts = new Accounts(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);

或者您在db_constants.inc.php中定义的常量被克隆。