What potential issues can arise when using PHP to interact with a MySQL database for user authentication and data manipulation?

Issue: SQL injection attacks can occur if user input is not properly sanitized before being used in SQL queries. Solution: Use prepared statements or parameterized queries to prevent SQL injection attacks.

// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

// Prepare a SQL query using prepared statements
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);

// Set parameters and execute the query
$username = $_POST['username'];
$password = $_POST['password'];
$stmt->execute();

// Check if user exists and password is correct
$result = $stmt->get_result();
if ($result->num_rows > 0) {
    // User authenticated successfully
} else {
    // Authentication failed
}

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