Vineet Mishra
Vineet Mishra

Reputation: 100

Getting Error - Warning: Missing argument 1 for DbQuery::__construct()

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();

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

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:

    <?php

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

?>

Thanks in advance.

Upvotes: 1

Views: 1011

Answers (1)

user1864610
user1864610

Reputation:

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.

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

You probably need

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

Upvotes: 2

Related Questions