我使用以下代码来创建与数据库的连接。但是给我的错误是警告:DbQuery :: __ construct()缺少参数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);
&GT;
提前致谢。
答案 0 :(得分:2)
这一行是你的问题:
$qry = new processQuery();
在您的课程中processQuery
扩展(indirecly)DBQuery
。 DbQuery构造函数由processQuery
继承,并且需要$ihost
,$iuser
,$ipass
和$idaba
的四个参数,但在实例化时不提供任何参数processUser
。
以前在实例化$db
两行时提供参数,但这是一个不同的对象,$qry
不会从中继承。
您可能需要
$qry = new processQuery('localhost', 'root', '', 'mkart');