致命错误:未捕获错误:找不到类“mysqli_connect”

问题描述 投票:0回答:1

请帮助我

致命错误:未捕获错误:在 C:\xampp\htdocs inal_program\php\loged_in.php:9 中找不到类“mysqli_connect” .php 第 9 行

我试图删除行开头的分号 (;) 以取消注释:

extension=mysqli

但是我的 php 代码仍然没有连接到我的数据库 phpmyadmin 它只显示此代码

致命错误:未捕获错误:在 C:\xampp\htdocs inal_program\php\loged_in.php:9 中找不到类“mysqli_connect” .php 第 9 行

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "management";

$conn = new mysqli_connect($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['login'])) {
    // Get username and password from form data and validate them
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Check if the username and password are correct
    // In this example, we're using a MySQLi prepared statement to protect against SQL injection
    $stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
    $stmt->bind_param("ss", $username, $password);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows == 1) {
        // If the user is authenticated, set the user's login status to true
        // In this example, we're just using a session variable to store the login status
        $_SESSION['logged_in'] = true;
    }
}

if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
    // If the user is logged in, display the success container by changing the CSS display property
    echo '<style>.success-container { display: block; }</style>';```
php database mysqli fatal-error
1个回答
0
投票

没有任何类称为

mysqli_connect
。根据 documentation,建立数据库连接是 procedural 风格。

请比较(取自同一页面中的示例):

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

你正在尝试使用 OOP,所以选择前者。

© www.soinside.com 2019 - 2024. All rights reserved.