2015-05-11 53 views
1

我正在使用以下代碼來創建與數據庫的連接。但是給我錯誤警告:缺少DbQuery :: __ construct()的參數1;獲取錯誤 - 警告:缺少DbQuery :: __構造函數的參數1()

請給我解決方案,並指出我與我的錯誤。以下是我的代碼:

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

>

在此先感謝。

+0

在這行你得到這個錯誤? –

回答

2

這條線是你的問題:

$qry = new processQuery(); 

在你的類processQuery延伸(indirecly)DBQuery。 DbQuery的構造函數由processQuery繼承,並且需要$ihost,$iuser,$ipass$idaba的四個參數,但在實例化processUser時不提供任何參數。

您在前兩行實例化$db時提供了參數,但這是一個不同的對象,並且$qry未從其繼承。

你可能需要

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

感謝您的回答。有用。 –