What is the best way to check if a given serial number exists in a database using PHP?

To check if a given serial number exists in a database using PHP, you can execute a SQL query to search for the serial number in the database table. If the query returns any rows, then the serial number exists in the database. You can use PHP's PDO (PHP Data Objects) or mysqli extension to interact with the database and perform the query.

<?php
// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Prepare a SQL query to check if the serial number exists in the database
$stmt = $pdo->prepare("SELECT * FROM your_table WHERE serial_number = :serial_number");
$stmt->bindParam(':serial_number', $serial_number);
$serial_number = 'ABC123';
$stmt->execute();

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