提问人:Sahar Kassaei 提问时间:3/18/2023 更新时间:3/18/2023 访问量:153
PHP 致命错误:未捕获错误:未找到类“dbConnection”[重复]
PHP Fatal error: Uncaught Error: Class "dbConnection" not found [duplicate]
问:
我正在尝试使用扩展“dbConnection”类的“Truck”类的函数,但我一直收到错误类“dbConnection”未找到。 我什至尝试包含它,但仍然遇到相同的错误。
truck.php类:
<?php
require_once './dbConnection.php';
class Truck extends dbConnection{
public $truckID;
public $driverName;
public $driverLastName;
public $plateNum;
public $availabilityCode;
function getAllTrucks(){
$sql= "SELECT * FROM truck";
$result = $this->connect()->query($sql);
$numRows = $result->num_rows;
if($numRows>0){
while($row = $result->fetch_assoc()){
$data[] = $row;
}
return $data;
} }}?>
dbConnection.php:
<?php
class dbConnection {
private $servername ;
private $username ;
private $password ;
private $db;
protected function connect(){
$this->servername="localhost";
$this->username="root";
$this->password="";
$this->db="smart_customer_services";
$conn = new mysqli($this->servername,$this->username,$this->password,$this->db);
return $conn;
}
}
?>
我在哪里称呼它:
<?php
$Trucks = new Truck();
$allTrucks= array();
$allTrucks= $Trucks->getAllTrucks();
?>
答:
-2赞
vikram kuldeep
3/18/2023
#1
请在没有功能的情况下尝试它,我认为它可以工作
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smart_customer_services";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM truck";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
$conn->close();
尝试使用函数
// db concation
class dbConnection {
protected function connect(){
$servername = 'localhost';
$username = 'root';
$password = '';
$db = 'smart_customer_services';
$conn = new MySQL($servername,$username,$password,$db);
return $conn;
}
}
class Truck extends dbConnection{
function getAllTrucks(){
require_once('dbConnection.php');
$result=$db->query("select * from truck");
$numRows = $result->num_rows;
if($numRows>0){
while($row = $result->fetch_assoc()){
$data[] = $row;
}
return $data;
}
}
}
评论
1赞
Your Common Sense
3/18/2023
卡车不是数据库连接。require_once不应该在课堂上使用。
上一个:获取多行并以 JSON 格式显示
评论