What are the potential security risks of using MySQLi for database connections in PHP?

Potential security risks of using MySQLi for database connections in PHP include SQL injection attacks if user input is not properly sanitized, as well as the risk of exposing sensitive database credentials if they are hardcoded in the code. To mitigate these risks, it is important to use parameterized queries and store database credentials securely.

<?php
// Secure database connection using MySQLi with parameterized queries

$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

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

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

// Sample query using parameterized query
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$username = "user_input";
$stmt->execute();
$result = $stmt->get_result();

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