What are the best practices for handling data retrieval and manipulation in PHP scripts?

When handling data retrieval and manipulation in PHP scripts, it is important to follow best practices to ensure efficiency and security. One key practice is to use prepared statements when interacting with databases to prevent SQL injection attacks. Additionally, it is recommended to validate and sanitize user input to avoid potential security vulnerabilities. Lastly, consider using object-oriented programming principles to organize and modularize your code for easier maintenance and scalability.

// Example of using prepared statements for data retrieval and manipulation
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Data retrieval using prepared statements
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->bindParam(':id', $userId, PDO::PARAM_INT);
$stmt->execute();
$userData = $stmt->fetch();

// Data manipulation using prepared statements
$stmt = $pdo->prepare('UPDATE users SET email = :email WHERE id = :id');
$stmt->bindParam(':email', $newEmail, PDO::PARAM_STR);
$stmt->bindParam(':id', $userId, PDO::PARAM_INT);
$stmt->execute();