我在尝试使用代码时遇到此错误:
警告:mysqli_query()期望参数1为mysqli,给定字符串 在第23行的D:\ AppServ \ www \ my \ classes \ db.class.php
警告:mysqli_error()期望参数1为mysqli,null给定 在第23行的D:\ AppServ \ www \ my \ classes \ db.class.php
db.class.php文件:
class DBclass{
private $servername;
private $username;
private $password;
private $dbname;
private $connect;
public function connectDB(){
$this->connect = mysqli_connect($this->servername,$this->username,$this->password,$this->dbname);
if(!$this->connect){
echo 'Error : Fail to connect to database !!' . mysqli_error($this->connect);
}
}
public function selectTBL($colum,$from,$where = '',$value = ''){
if($where == '' && $value == ''){
$query = 'SELECT '.$colum.'FROM '.$from;
}elseif($where != '' && $value != ''){
$query = 'SELECT '.$colum.'FROM '.$from.' WHERE '.$where. ' = '.$value;
}
mysqli_query($query,$this->connect) or die(mysqli_error($this->connect));
$row = mysqli_fetch_array($query);
return $row[$colum];
}
}
home.html文件:
<?php $query = new DBclass; ?>
<html dir="rtl">
<head>
<title>title</title>
</head>
<body>
<?php $query->selectTBL('VALUE','SITE_CONFIGS','CONFIG','SITE_TITLE'); ?>
</body>
</html>
答案 0 :(得分:1)
public function selectTBL($colum,$from,$where = '',$value = '')
{
if( is_null($this->connect))
$this->connectDB();
if($where == '' && $value == ''){
$query = 'SELECT '.$colum.'FROM '.$from;
}elseif($where != '' && $value != ''){
$query = 'SELECT '.$colum.'FROM '.$from.' WHERE '.$where. ' = '.$value;
}
$result = mysqli_query($this->connect, $query) or die(mysqli_error($this->connect));
$row = mysqli_fetch_array($result);
return $row[$colum];
}