What is the significance of using MySQL databases for user authentication in PHP?

Using MySQL databases for user authentication in PHP is significant because it allows for secure storage and retrieval of user credentials. By storing hashed passwords in a database, it helps protect sensitive user information from unauthorized access. Additionally, MySQL databases provide efficient querying capabilities for checking user credentials during the authentication process.

// Establish a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

// Retrieve user credentials from the database
$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM users WHERE username='$username'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    if (password_verify($password, $row['password'])) {
        // User authentication successful
        echo "Login successful!";
    } else {
        // Invalid password
        echo "Invalid password";
    }
} else {
    // User not found
    echo "User not found";
}

// Close the database connection
$conn->close();