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
https://forums.phpfreaks.com/topic/157421-nothings-happening-to-bookorama/
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 ...

Archived

This topic is now archived and is closed to further replies.

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