Help with php/MySql query
Posted: Fri Oct 21, 2005 1:00 pm
I'm fairly new to the world of PHP and have been working with the book, "PHP & MySQL Web Development" by Luke Welling and Laura Thomson and so far, it is a great book. The script that I am having problems with comes directly from that book so for any of you that may have that book, the chapters I am dealing with are chapter 10 - 11.
My site is being hosted and the version of PHP installed on the server is 4.3
So basically, I created a MySQL database named "books" and inserted the required tables and fields. This database "books" is a catalog of books (title, ISBN) and customer information.
The page "search.html" is a simple form which allows a query to be performed. You can search the ISBN, Author, or book title. The html code is below:
Here is the PHP/MySQL code for "results.php" This is what has been giving me problems:
So what happens when I search is that the "results.php" comes back with absolutely nothing, not even an error message. When I change the name of the database so that the script will not be able to connect, it still does not give me a connection error. I find that to be very odd. I know that this script uses the mysqli library which is object-oriented, and PHP version 4 and above should support that so I don't see why that could be a problem. I substituted procedural syntaxes for the mysqli to see if it would work and it still did the same thing--returned a page with absolutely nothing on it.
Does anybody know what's going on? It would help me so much to figure this out!! Thanks a lot.
My site is being hosted and the version of PHP installed on the server is 4.3
So basically, I created a MySQL database named "books" and inserted the required tables and fields. This database "books" is a catalog of books (title, ISBN) and customer information.
The page "search.html" is a simple form which allows a query to be performed. You can search the ISBN, Author, or book title. The html code is below:
Code: Select all
<html>
<head>
<title>Book-O-Rama Catalog Search</title>
</head>
<body>
<h1>Book-O-Rama Catalog Search</h1>
<form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author</option>
<option value="title">Title</option>
<option value="isbn">ISBN</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text">
<br />
<input type="submit" value="Search">
</form>
</body>
</html>Code: Select all
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = mysqli_connect('localhost', 'username', 'password', 'domain_com_-_books');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = mysqli_query($db, $query);
$num_results = $result->num_rows;
echo '<p>Number of books found: '.$num_results.'</p>';
for ($i=0; $i <$num_results; $i++)
{
$row = mysqli_fetch_assoc($result);
echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row['title']));
echo '</strong><br />Author: ';
echo stripslashes($row['author']);
echo '<br />ISBN: ';
echo stripslashes($row['isbn']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '</p>';
}
$row = mysqli_fetch_object($result);
$db->close();
?>
</body>
</html>Does anybody know what's going on? It would help me so much to figure this out!! Thanks a lot.