PHP MYSQL

SEARCH & DISPLAY RESULTS FROM DATABASE

HTML SEARCH FORM <form method="post">   <input type="text" name="search"/>   <input type="submit" value="Search"/> </form>

HTML SEARCH FORM

01

SEARCH WITH PHP ON SUBMIT if (isset($_POST["search"])) {   require "search.php"; }

CONNECT TO DATABASE $pdo = new PDO(   "mysql:host=HOST;charset=utf8;   dbname=NAME", USER, PASSWORD,   [PDO::ATTR_ERRMODE =>   PDO::ERRMODE_EXCEPTION,   PDO::ATTR_DEFAULT_FETCH_MODE    => PDO::FETCH_ASSOC] );

PHP-MYSQL SEARCH (A)

02

SEARCH QUERY $stmt = $pdo->prepare("SELECT * FROM `TABLE` WHERE `FIELD` LIKE ?"); $stmt->execute (["%". $_POST["search"] ."%"]); $results = $stmt->fetchAll();

OUTPUT RESULTS foreach ($results as $r) { echo "<div>". $r["FIELD"] ."</div>"; }

PHP-MYSQL SEARCH (B)

03