Pedestrian Posted May 8, 2009 Share Posted May 8, 2009 I am starting out on PHP/MYSQL using the book "PHP and MYSQL Web Development" and I'm having trouble running this code: <html> <head> <title>Book-O-Rama Search Results</title> </head> <body> <h1>Book-O-Rama Search Results</h1> <?php error_reporting(E_ALL) ; ini_set('display_errors',1); ?> <?php // create short variable names echo 'I am here1.'; $searchtype=$_POST['searchtype']; $searchterm=trim ($_POST['searchterm']); if (!$searchtype || !$searchterm) { echo 'I am here2.'; echo 'You have not entered search details. Please go back and try again.'; exit; } if (!get_magic_quotes_gpc()) { echo 'I am here3.'; $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); } echo 'I am here4.'; @ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books'); echo 'I am here5.'; if (mysqli_connect_errno()) { echo 'I am here6'; echo 'Error: Could not connect to database. Please try again later.'; exit; } echo 'I am here7.'; $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo 'I am here8.'; echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); 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>'; } echo 'I am here9.'; $result->free(); $db->close(); echo 'I am here10.'; ?> </body> </html> The program only goes as far as "I am here4" ... and nothing happens. I don't get any errors, just a blank screen. Is it unable to gain access to the database? This is the database: *************************** 1. row *************************** isbn: 0-672-31509-2 author: Pruitt, et al. title: Teach Yourself GIMP in 24 Hours price: 24.99 *************************** 2. row *************************** isbn: 0-672-31697-8 author: Michael Morgan title: Java 2 for Professional Developers price: 34.99 *************************** 3. row *************************** isbn: 0-672-31745-1 author: Thomas Down title: Installing Debian GNU/Linux price: 24.99 *************************** 4. row *************************** isbn: 0-672-31769-9 author: Thomas Schenk title: Caldera OpenLinux System Administration Unleashed price: 49.99 4 rows in set (0.02 sec) Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Hello, Please don't color your code. It makes it very hard to read. Please use tags. Quote Link to comment Share on other sites More sharing options...
Pedestrian Posted May 9, 2009 Author Share Posted May 9, 2009 Sorry! I've pasted the code here again! <html> <head> <title>Book-O-Rama Search Results</title> </head> <body> <h1>Book-O-Rama Search Results</h1> <?php // create short variable names echo 'I am here1.'; $searchtype=$_POST['searchtype']; $searchterm=trim ($_POST['searchterm']); if (!$searchtype || !$searchterm) { echo 'I am here2.'; echo 'You have not entered search details. Please go back and try again.'; exit; } if (!get_magic_quotes_gpc()) { echo 'I am here3.'; $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); } echo 'I am here4.'; @ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books'); echo 'I am here5.'; if (mysqli_connect_errno()) { echo 'I am here6'; echo 'Error: Could not connect to database. Please try again later.'; exit; } echo 'I am here7.'; $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo 'I am here8.'; echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); 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>'; } echo 'I am here9.'; $result->free(); $db->close(); echo 'I am here10.'; ?> </body> </html> And the database: *************************** 1. row *************************** isbn: 0-672-31509-2 author: Pruitt, et al. title: Teach Yourself GIMP in 24 Hours price: 24.99 *************************** 2. row *************************** isbn: 0-672-31697-8 author: Michael Morgan title: Java 2 for Professional Developers price: 34.99 *************************** 3. row *************************** isbn: 0-672-31745-1 author: Thomas Down title: Installing Debian GNU/Linux price: 24.99 *************************** 4. row *************************** isbn: 0-672-31769-9 author: Thomas Schenk title: Caldera OpenLinux System Administration Unleashed price: 49.99 4 rows in set (0.02 sec) Sorry about that ... Quote Link to comment Share on other sites More sharing options...
Pedestrian Posted May 9, 2009 Author Share Posted May 9, 2009 I forgot to add that when I take the error suppressor off, I get this: Fatal error: Class 'mysqli' not found in C:\Apache2\htdocs\results.php on line 32 and I have no idea what I'm supposed to do about it! ??? Quote Link to comment Share on other sites More sharing options...
timt Posted May 9, 2009 Share Posted May 9, 2009 That means you do not have that class of functions available. Use mysql. Remove @ sign and try this. $db = mysql_connect( 'localhost', 'bookorama', 'bookorama123' ); Quote Link to comment Share on other sites More sharing options...
Pedestrian Posted May 9, 2009 Author Share Posted May 9, 2009 Wow ... then I am really worse off then I thought. When I do that, I get this message: Fatal error: Call to undefined function mysql_connect() in C:\Apache2\htdocs\results.php So PHP does not at all recognize MYSQL functions? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 9, 2009 Share Posted May 9, 2009 Please read this http://www.phpfreaks.com/forums/index.php/topic,95378.0.html Quote Link to comment Share on other sites More sharing options...
Pedestrian Posted May 10, 2009 Author Share Posted May 10, 2009 Thanks for the link. I tried that, but now Apache crashes when I run the code. I get the following error: "Apache HTTP Server Apache HTTP Server has encountered a problem and needs to close. " Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.