When migrating PHP code to adhere to modern standards, what steps can be taken to update database interactions from deprecated functions like mysql_query to more secure alternatives?
When migrating PHP code to adhere to modern standards, one important step is to update database interactions from deprecated functions like mysql_query to more secure alternatives like PDO or MySQLi. This helps prevent SQL injection attacks and improves the overall security of the application.
// Using PDO for database interactions
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
while ($row = $stmt->fetch()) {
// Process the retrieved data
}