What are some best practices for retrieving specific values from MySQL queries in PHP?

When retrieving specific values from MySQL queries in PHP, it is best practice to use prepared statements to prevent SQL injection attacks and ensure data integrity. Additionally, using appropriate error handling techniques can help identify and resolve any issues that may arise during the query execution.

// Example of retrieving specific values from a MySQL query in PHP

// Establish a database connection
$connection = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Prepare and execute the query
$stmt = $connection->prepare("SELECT column_name FROM table_name WHERE condition = ?");
$stmt->bind_param("s", $condition);
$condition = "value";
$stmt->execute();
$result = $stmt->get_result();

// Fetch the specific value
if ($row = $result->fetch_assoc()) {
    $specific_value = $row['column_name'];
}

// Close the statement and connection
$stmt->close();
$connection->close();

// Use the retrieved specific value as needed
echo $specific_value;