What are some potential security risks associated with using the MySQL extension in PHP?
One potential security risk associated with using the MySQL extension in PHP is the vulnerability to SQL injection attacks. To mitigate this risk, it is recommended to use parameterized queries or prepared statements to sanitize user input before executing SQL queries.
// Example of using prepared statements to prevent SQL injection
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Prepare a SQL statement
$stmt = $mysqli->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
// Bind parameters
$stmt->bind_param("ss", $username, $password);
// Set parameters and execute
$username = "user1";
$password = "password1";
$stmt->execute();
// Close statement and connection
$stmt->close();
$mysqli->close();