获取错误——警告:DbQuery: __construct()缺少参数1

时间:2022-02-07 02:24:39

I am using the following code to for creating connection to database. But is giving me error as Warning: Missing argument 1 for DbQuery::__construct();

我正在使用下面的代码来创建与数据库的连接。但是给我的错误是警告:DbQuery::__construct()的缺失参数1;

Kindly give me the solution and point me out with my mistake. Following is my code:

请给我解决方案,并指出我的错误。下面是我的代码:

core.php

core.php

    //Database connection
class DbQuery{
    public $conn;
    private $host;
    private $user;
    private $pass;
    private $daba;

    public function __construct($ihost, $iuser, $ipass, $idaba){
        $this->host = $ihost;
        $this->user = $iuser;
        $this->pass = $ipass;
        $this->daba = $idaba;

        $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->daba);

        if($this->conn->connect_errno){
            die("Failed to connect with database : (" . $this->conn->connect_errno . ")" . $this->conn->connect_errno);
        }
    }
}
    class GenQuery extends DbQuery{
    //EXECUTE QUERY
    function exeQuery($input){
        $result = mysqli_query($this->conn, $input);
        if(!$result){
            die("Invalid query : ". mysqli_error($this->conn));
        }
        return $result;
    }
    //SELECT QUERY
    function selQuery($selectItem, $tableName, $condName, $condValue){
        $n = sizeof($selectItem); 
        $m = sizeof($condValue);
        $l = sizeof($condName); 

        if($m == $l){
            for($j=0; $j<$n; $j++){
                if($j == 0){
                    $selectVal = $selectItem[$j];
                }
                else{
                    $selectVal .= ", " . $selectItem[$j];;
                }
            }

            for($i=0; $i<$m; $i++){
                if($i == 0){
                    $val = $condName[$i] . " = '" . $condValue[$i] . "'";
                }
                else{
                    $val .= " AND " . $condName[$i] . " = '" . $condValue[$i] . "'";
                }
            }
            $query = "SELECT " . $selectVal . " FROM " . $tableName . " WHERE " . $val;
            $result = $this->exeQuery($query);
            return $result;
        }
    }
    }

   class ProcessQuery extends GenQuery{
    function selUser($condValue){
        $selectItem = array('*');
        $condName = array('uid');
        $input = $this->selQuery($selectItem, 'mk_user', $condName, $condValue);
        if(mysqli_num_rows($input) > 0){
            while($frow = mysqli_fetch_assoc($input)){
                $res = $frow['uname'];
            }
            return $res;
        }
    }
 }

test.php:

test.php:

    <?php

require 'core.php';
$db = new DbQuery('localhost', 'root', '', 'mkart');
$b = ['12'];
$qry = new processQuery();
echo $res = $qry->selUser($b);

?>

? >

Thanks in advance.

提前谢谢。

1 个解决方案

#1


2  

This line is your problem:

这条线是你的问题:

$qry = new processQuery();

In your classes processQuery extends (indirecly) DBQuery. The DbQuery constructor is inherited by processQuery and requires four parameters for $ihost, $iuser, $ipass and $idaba, but you're not providing any when you instantiate processUser.

在您的类中,processQuery扩展(indirecly) DBQuery。DbQuery构造函数由processQuery继承,需要$ihost、$iuser、$ipass和$idaba四个参数,但在实例化processUser时不提供任何参数。

You're providing parameters when you instantiate $db two lines before, but that's a different object, and $qry doesn't inherit from it.

当您之前实例化$db两行时,您将提供参数,但是这是一个不同的对象,并且$qry不会继承它。

You probably need

你可能需要

$qry = new processQuery('localhost', 'root', '', 'mkart');

#1


2  

This line is your problem:

这条线是你的问题:

$qry = new processQuery();

In your classes processQuery extends (indirecly) DBQuery. The DbQuery constructor is inherited by processQuery and requires four parameters for $ihost, $iuser, $ipass and $idaba, but you're not providing any when you instantiate processUser.

在您的类中,processQuery扩展(indirecly) DBQuery。DbQuery构造函数由processQuery继承,需要$ihost、$iuser、$ipass和$idaba四个参数,但在实例化processUser时不提供任何参数。

You're providing parameters when you instantiate $db two lines before, but that's a different object, and $qry doesn't inherit from it.

当您之前实例化$db两行时,您将提供参数,但是这是一个不同的对象,并且$qry不会继承它。

You probably need

你可能需要

$qry = new processQuery('localhost', 'root', '', 'mkart');