function customerExists($id) { $pdo = new PDO("mysql:host=localhost;dbname=myDB", "myUser", "myPassword"); $stmt = $pdo->prepare("SELECT COUNT(*) FROM customers WHERE id = ?"); $stmt->execute([$id]); $count = $stmt->fetchColumn(); return ($count > 0); }
function customerExists($id) { $mysqli = new mysqli("localhost", "myUser", "myPassword", "myDB"); $stmt = $mysqli->prepare("SELECT COUNT(*) FROM customers WHERE id = ?"); $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($count); $stmt->fetch(); return ($count > 0); }In this example, we are using the MySQLi library to connect to a MySQL database, prepare a SELECT statement to count the number of customers with the given ID, bind the ID parameter to the statement, execute the statement, bind the resulting count to a variable, fetch the count, and return whether the count is greater than 0 or not. Package/library: PDO or MySQLi.