What are the best practices for checking if a specific value exists in a MySQL database using PHP?

When checking if a specific value exists in a MySQL database using PHP, it is best practice to use a prepared statement to prevent SQL injection attacks. This involves using placeholders in the query and binding the actual value to the placeholder before executing the query. Additionally, fetching the result and checking if any rows are returned can determine if the value exists in the database.

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

// Prepare a statement to check if a specific value exists in a table
$stmt = $pdo->prepare("SELECT * FROM table WHERE column = :value");
$stmt->bindParam(':value', $specificValue, PDO::PARAM_STR);
$specificValue = 'desired_value';
$stmt->execute();

// Check if any rows are returned
if($stmt->rowCount() > 0) {
    echo "Value exists in the database.";
} else {
    echo "Value does not exist in the database.";
}
?>