What are the best practices for retrieving and using values from a database in PHP to avoid manual adjustments?

When retrieving values from a database in PHP, it's important to use prepared statements to prevent SQL injection attacks and ensure data integrity. Additionally, using parameterized queries can help avoid the need for manual adjustments when dealing with special characters or formatting issues. Finally, storing database connection details in a separate configuration file can make it easier to update credentials without having to manually adjust code.

<?php
// Include database connection configuration
include 'config.php';

// Create a PDO connection to the database
try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Error: " . $e->getMessage());
}

// Prepare a parameterized query to retrieve values from the database
$stmt = $pdo->prepare("SELECT * FROM table WHERE column = :value");
$stmt->bindParam(':value', $value);
$stmt->execute();

// Fetch the results and use them in your application
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Use the values retrieved from the database
    echo $row['column_name'];
}
?>