Help with database book example...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Buggies
Forum Newbie
Posts: 7
Joined: Wed Aug 31, 2005 1:35 pm

Help with database book example...

Post by Buggies »

Hello folks new to PHP and this forums. Thanks for any help on this.

I'm reading a book called "PHP and MySQL Web Development", and I'm stuck on an example that doesn't not seem to work... on my machine or on an F2O.org free account. Here is the code:

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.';
     echo '</body></html>';
         exit;
  }
  
  if (!get_magic_quotes_gpc())
  {
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
  }

  @ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');

  if (mysqli_connect_errno()) 
  {
     echo 'Error: Could not connect to database.  Please try again later.';
     echo '</body></html>';
         exit;
  }

  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = $db->query($query);

  $num_results = $result->num_rows;

  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>';
  }
  
  $result->free();
  $db->close();

?>
</body>
</html>
It prints out the <h1>Book-O-Rama Search Results</h1>, and then it doesn't seems to spit anything else back... not even the closing body or html tags. Can anyone help me on this? This might have something to do with another issue I had here: viewtopic.php?t=37583
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

check your error logs.
Buggies
Forum Newbie
Posts: 7
Joined: Wed Aug 31, 2005 1:35 pm

Post by Buggies »

PHP Fatal error: Cannot instantiate non-existent class: mysqli
Same problem. I'm stumped now. Doesn't work on my server... or f2o server. This book blows. I don't know what to do now :?:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well, the example isn't doing anything the old mysql extension couldn't do, so you could convert it back to mysql. The mysqli and mysql extensions have very similar set up's so you should be able to figure it out pretty quickly. Post back if you need a bit of help..
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Wouldn't you need a require_once( "/path/to/file/mysqli.php") style statement somewhere before the new statement?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysqli is a built-in object created by the mysqli extension, so nope. :)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well then the extension isn't loaded.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

exactly, which is the same conclusion of his thread in "web servers." :?
Buggies
Forum Newbie
Posts: 7
Joined: Wed Aug 31, 2005 1:35 pm

Post by Buggies »

Problem solved!
Thanks for the help!
Post Reply