What are the best practices for handling database values in PHP to avoid the need for searching in arrays?

When handling database values in PHP, it is best practice to use SQL queries to retrieve specific data instead of fetching large datasets and searching within arrays. This approach is more efficient and reduces the need for complex array manipulation in PHP code.

// Example of fetching specific data from a database using SQL query
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Fetching a specific user by ID
$user_id = 1;
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $user_id);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// Now $user contains the data of the user with ID 1