What best practices should be followed when retrieving data from a database in PHP?

When retrieving data from a database in PHP, it is important to use parameterized queries to prevent SQL injection attacks. Additionally, it is recommended to sanitize user input and validate data to ensure the security and integrity of the database.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a parameterized query
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');

// Bind the parameter
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

// Execute the query
$stmt->execute();

// Fetch the results
$user = $stmt->fetch();