Advertisement
Guest User

Script Retrieves Search Results from the MySQL Database

a guest
Apr 24th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Book-O-Rama Search Results</title>
  6. </head>
  7.  
  8. <body>
  9. <h1>Book-O-Rama Search Results</h1>
  10. <?php
  11. // create short variable names
  12. $searchtype=$_POST['searchtype'];
  13. $searchterm=trim($_POST['searchterm']);
  14. if (!$searchtype || !$searchterm) {
  15. echo 'You have not entered search details. Please go back and try again.';
  16. exit;
  17. }
  18. if (!get_magic_quotes_gpc()){
  19. $searchtype = addslashes($searchtype);
  20. $searchterm = addslashes($searchterm);
  21. }
  22. $dbhost = '213.171.200.57'; // your database server’s IP address
  23. $dbuser = 'bookorama'; // the database username
  24. $dbpass = 'password'; // the database password
  25. $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('error connecting to
  26. your database'); // opens a connection to the server or gives an error
  27. $dbname = 'bookorama'; // the database name
  28. mysql_select_db($dbname); // connects to your database
  29.  
  30. $query = "SELECT * FROM books WHERE ".$searchtype." like '%".$searchterm."%'";
  31. $result = $db->query($query);
  32. $num_results = $result->num_rows;
  33. echo "<p>Number of books found: ".$num_results."</p>";
  34. for ($i=0; $i <$num_results; $i++) {
  35. $row = $result->fetch_assoc();
  36. echo "<p><strong>".($i+1).". Title: ";
  37. echo htmlspecialchars(stripslashes($row['title']));
  38. echo "</strong><br />Author: ";
  39. echo stripslashes($row['author']);
  40. echo "<br />ISBN: ";
  41. echo stripslashes($row['isbn']);
  42. echo "<br />Price: ";
  43. echo stripslashes($row['price']);
  44. echo "</p>";
  45. }
  46. $result->free();
  47. $db->close();
  48. ?>
  49. </body>
  50. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement