你好,我做了PHP類爲我的數據庫,我使用的XAMPP在Windows 10PHP數據庫類,或使用類的代碼是不工作
似乎什麼是錯的,我不能發現問題,我不知道它在我的類文件或我inex.php文件
這做的是錯誤:
致命錯誤:未捕獲的錯誤:類「DB」在d未找到: \ XAMP \ htdocs中\ telepol \ userInfo.php:5堆棧跟蹤:#0 d:\ XAMP \ htdocs中\ telepol \ userInfo.php(43):showData()#1 {主}拋出d:\ XAMP \ htdocs中\第5行telepol \ userInfo.php
db.php中
<? php
class db{
private $_conn;
private $_host = "localhost";
private $_username = "root";
private $_psw = "";
private $_dbName = "users";
public function __construct(){
$this->_conn = new mysqli($this->_host, $this->_username, $this->_psw, $this->_dbName);
if ($this->_conn->connect_error){
die("Connection failed: " . $this->_conn->connect_error);
}
}
public function getCon(){
return $this->_conn;
}
}
?>
userInfo.php
<?php
require_once 'classes/db.php';
function showData(){
$conn = new db();
$br = 1; //br is used to displat the number of each row in the table.
$userQuery = "SELECT* FROM users";
$result = $conn->getCon()->query($userQuery);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr> <td>" .$br ."</td> <td>" .$row["first_name"] ."</td> <td>" .$row["last_name"] ."</td> <td>" .$row["nickname"] ."</td> <td>" .$row["user_id"] ."</td> <br>" ;
$br ++;
}
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Users Info</title>
<!-- adding JS scripts -->
<script type="text/javascript" src="jquery-3.1.1.min.js"></script>
<!-- adding css -->
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<!-- my CSS -->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<table class="table table-striped table-bordered table-hover table-sm">
<thead class="thead-default">
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nickname</th>
<th>User ID</th>
</tr>
</thead>
<?php showData(); ?>
</table>
</body>
</html>
我不知道,如何解決它。我試過 '$ database = new db(); $ conn = $ database-> getCon(); ' 還是老樣子不工作 –
這可能是因爲你的'__construct'功能是私人在'db.php' –
在這裏其他方面的建議以及實例您的mysqli類,當你有一個錯字。你有msqli,應該是mysqli,你錯過了「y」。 – Jase