Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset='utf-8' />
- <title>Airports demo with PDO Prepared statements</title>
- </head>
- <body>
- <h1>Airports demo with PDO Prepared statements</h1>
- <?php
- //Initialise errors array. This is the array in which catch all the errors, if any, and print them out at the end of the script
- $errors = array();
- echo"<form action='' method='GET'>
- <label for='search'>Search for airports in country</label>
- <input type='text' id='search' name='search' />
- <input type='submit' value='search' />
- </form>";
- try{
- //Create a new PDO database connection
- $db = new PDO('sqlite:test.db');
- //Set this attribute while testing so PDO doesn't silence error reporting
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- //Here is the prepared statement
- if(!empty($_GET['search'])){
- if($statement = $db->prepare("SELECT name, city, country FROM airports WHERE country = ?")){
- //The values in this array are bound to the question mark in the prepared SELECT
- $binding = array("{$_GET['search']}%");
- $statement -> execute($binding);
- //Make sure we get the associative array
- $result = $statement -> fetchall(PDO::FETCH_ASSOC);
- //Check for results
- foreach($result as $item => $val){
- echo "<p>{$val['name']} in {$val['city']}</p>";
- }
- }
- }
- }
- catch(PDOException $e){
- $errors[]=$e->getMessage();
- }
- if(count($errors) > 0){
- echo "<p>Oooops we have errors!</p>";
- echo "<ul>";
- foreach($errors As $error){
- echo "<li>{$error}</li>";
- }
- echo "</ul>";
- }
- ?>
- </body>
- </html>
Add Comment
Please, Sign In to add comment