所以我是PHP的初学者,并且正在使用MySQL制作一个简单的CRUD应用程序。制作CRUD应用程序表已完成,并且已成功将其与我的MySQL数据库连接。但是,当我在服务器上加载页面时,遇到了无法调试的错误。这是我遇到错误的CRUD表的代码的一部分(代码的57-74行):
// Attempt select query execution my
$sql = "SELECT * FROM students";
if($result = $mysqli->query($sql)){
if($result->num_rows > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>Name</th>";
echo "<th>Father's Name</th>";
echo "<th>Mother's Name</th>";
echo "<th>Email</th>";
echo "<th>Contact</th>";
echo "<th>Alt. Contact</th>";
echo "<th>Alt. Contact</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
这是我得到的错误:
Notice: Undefined variable: mysqli in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php on line 59
Fatal error: Uncaught Error: Call to a member function query() on null in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php:59 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php on line 59
您丢失了PHP和MySQL之间数据库连接的最重要的一行。 mysqli
类,表示PHP和MySQL数据库之间的连接。另外,启用mysqli
错误报告,以便您可以轻松找出错误。查看更多:https://www.php.net/manual/en/mysqli.query.php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("your_host", "your_user", "your_password", "your_database");
// Change character set to utf8
$mysqli->set_charset("utf8mb4");