What potential security risks are associated with using the MySQL extension in PHP?
Using the MySQL extension in PHP can pose security risks such as SQL injection attacks if user input is not properly sanitized. To mitigate this risk, it is recommended to use parameterized queries or prepared statements to safely interact with the database.
// Connect to MySQL database using PDO with prepared statements
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
// Prepare a SQL query
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
// Bind parameters and execute the query
$stmt->bindParam(':username', $username);
$stmt->execute();
// Fetch results
$results = $stmt->fetchAll();