Jump to content

Nothing's happening to bookorama ...


Pedestrian

Recommended Posts

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)

 

 

 

 

Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

That means you do not have that class of functions available.  Use mysql.

 

Remove @ sign and try this.

 


   $db = mysql_connect( 'localhost', 'bookorama', 'bookorama123' );

Link to comment
Share on other sites

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?  :-[

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.