Trouble with querying database w/form data for "Learning PHP & MySQL" book

So I purchased this book to help me learn PHP. As of today, I’ve hit a brick wall and I want to know if its me or the book that is wrong. I’ve been through every tutorial in the book up to this point. I’ve successfully queried SQL statements in the localhost database called “test”. Its pretty much 3 tables (books, authors, purchases)… very very simple stuff. By doing another tutorial, I know that PEAR is successfully running as well (i’ve installed xampp).

Here’s the problem. I’m on the tutorial called “Querying the database with Form Data”. I’ve followed along and wrote the code out exactly… and its telling me that I should have two results after hitting the submit button. Is there anything in my code that has depreciated? Is my book out-dated? Does anyone else suggest another tutorial that I can use to learn how to search the database?

Here is the code:

[php]

<?php function query_db($qstring) { include('db_login.php'); require_once('DB.php'); $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database"); if (DB::isError($connection)){ die("Could not connect:
". DB::errorMessage($connection)); } if(get_magic_quotes_gpc()) { // guard against SQL injection $qstring = stripslashes($qstring); } $query = "SELECT title, pages FROM books WHERE books.title LIKE '%$qstring%'"; // builds the query $qstring = mysql_query($query) or die('error'); $result = mysql_fetch_assoc($qstring); if (DB::isError($result)){ die("Could not query the database:
$query ".DB::errorMessage($result)); } echo ''; echo ''; while ($result_row = $result->fetchRow()) { echo "'; } echo '
title Author Pages
"; echo $result_row[1] . ' '; echo $result_row[3] . ' '; echo $result_row[2] . '
'; $connection->disconnect(); } ?> Building a form <?php $search = htmlentities($GET["search"]); $self = htmlentities($SERVER['PHP_SELF']); if ($search != NULL) { echo "The search string is: $search</strong."; query_db($search); } else { echo (' Search: '); } ?> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service