13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

270 Chapter 11 Accessing Your <strong>MySQL</strong> Database from the <strong>Web</strong> with <strong>PHP</strong><br />

Listing 11.2<br />

Continued<br />

if (!$searchtype || !$searchterm) {<br />

echo 'You have not entered search details. Please go back <strong>and</strong> try again.';<br />

exit;<br />

}<br />

if (!get_magic_quotes_gpc()){<br />

$searchtype = addslashes($searchtype);<br />

$searchterm = addslashes($searchterm);<br />

}<br />

@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');<br />

if (mysqli_connect_errno()) {<br />

echo 'Error: Could not connect to database. Please try again later.';<br />

exit;<br />

}<br />

$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";<br />

$result = $db->query($query);<br />

$num_results = $result->num_rows;<br />

echo "Number of books found: ".$num_results."";<br />

for ($i=0; $i fetch_assoc();<br />

echo "".($i+1).". Title: ";<br />

echo htmlspecialchars(stripslashes($row['title']));<br />

echo "Author: ";<br />

echo stripslashes($row['author']);<br />

echo "ISBN: ";<br />

echo stripslashes($row['isbn']);<br />

echo "Price: ";<br />

echo stripslashes($row['price']);<br />

echo "";<br />

}<br />

$result->free();<br />

$db->close();<br />

?><br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!