我有一个名为dbcontroller.php
的文件。此文件是具有mysql
函数的旧文件。但现在我正在尝试将其转换为mysqli
函数。一切正常,但$conn
mysqli_connect($conn,$query)
发出错误。如何解决这个问题?
代码
class DBController {
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "stores";
/**
* DBController constructor.
*/
public function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
function connectDB() {
$conn = mysqli_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn) {
mysqli_select_db($conn,$this->database);
}
function runQuery($query) {
$result = mysqli_query($conn,$query);// error
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function numRows($query) {
$result = mysqli_query($conn,$query);// error
$rowcount = mysqli_num_rows($result);
return $rowcount;
}
}
答案 0 :(得分:1)
我认为问题在于:
public function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
此处$conn
必须是使用其他变量定义的类变量,可以像以下一样访问:
$this->conn