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
}
Related Questions
- What alternative PHP functions can be used for accurate character length calculations in multibyte strings?
- What potential visual problems could arise from outputting HTML tables in PHP scripts?
- How can PHP developers ensure that inline images are properly displayed in emails instead of being sent as attachments?