Try It Yourself Here...
Country:
Start Typing to Search...

<!--BEGIN HTML FORM-->
<form action="" method="post">
<p>Country: <input type="text" name="countrysearch" />
<input type="submit" value="Search" /></p>
</form>
<!--END HTML FORM - BEGIN PHP CODE-->
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['countrysearch']) && strlen(trim($_POST['countrysearch']))) {
$search = $_POST['countrysearch'];
$dsn = 'mysql:dbname=testdb;host=localhost';
$user = 'testuser';
$password = 'testpass';
try {
$dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8; SET CHARACTER SET utf8"));
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $dbh->prepare("SELECT Country.Code AS CountryCode, Country.Name AS CountryName FROM Country WHERE Country.Name LIKE :search ORDER BY Country.Name ASC LIMIT 0,10");
$stmt->bindParam(':search', $search, PDO::PARAM_STR);
$search = trim($search); # Remove all whitespace before and after the string
// Escape wildcard characters for MySQL Query
$search = str_replace('%','\%',$search);
$search = str_replace('_','\_',$search);
$search = "$search%"; # Add wild card character at the end to match all possibilities starting with the search phrase
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
echo "<p>There was an <b>error</b> connecting to the database!<br />Please try your request again later...</p>\r\n";
// Do your logging and recovery here...
customLogFunction($e->getMessage());
}
if (!$stmt->rowCount()) {
echo "<p><em>No results found...</em></p>";
}
else {
echo "Found {$stmt->rowCount()} possible matches...<br/>\r\n<ul>\r\n";
foreach ($rows as $row) {
echo "<li>{$row[CountryName]}</li>\r\n";
}
echo "</ul>\r\n";
}
}
?>