What are common syntax errors to watch out for when writing PHP code for database connections and user registration/login functionality?

One common syntax error when writing PHP code for database connections is forgetting to include the necessary credentials such as the database host, username, password, and database name. To solve this, make sure to define these variables correctly before attempting to establish a connection. Another common error when implementing user registration/login functionality is not properly sanitizing user input to prevent SQL injection attacks. To address this, use prepared statements or parameterized queries to securely interact with the database.

// Database connection
$host = "localhost";
$username = "root";
$password = "";
$database = "my_database";

$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// User registration/login functionality
$username = $_POST['username'];
$password = $_POST['password'];

// Sanitize input to prevent SQL injection
$username = mysqli_real_escape_string($conn, $username);
$password = mysqli_real_escape_string($conn, $password);

// Use prepared statement to securely interact with the database
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->close();