Jump to content

[SOLVED] Results showing blank page...


mcclellanfsu

Recommended Posts

Hello, I have a simple html search form. I want to select what I will search for and then some search criteria. I am not getting any errors, but my problem is that nothing will show up...not even the count.

 

Here are my two pages of code:

 

search.html

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

 

and results.php

 

<?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=mysql_connect ("localhost", "xxxxxx_booktst", "xxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
      mysql_select_db ("xxxxxx_bookorama");

  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = mysql_query($query); //Change made here -- used to be $db->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>';
  }

//mysql_free_result($result); Wouldn't work so I commented it out for now.
mysql_close();  

?>
</body>

 

Thank you in advance for any help you can give me!

Link to comment
https://forums.phpfreaks.com/topic/39659-solved-results-showing-blank-page/
Share on other sites

I am unsure where the following line comes from. It seems that $num_results is not getting set and therefore the for loop refuses to run.

$num_results = $result->num_rows;

Try changing that to

$num_results = mysql_num_rows($result);

 

Maybe I am missing something. Excuse me if I am.

Thank you for your responses. That did fix a couple of the problems, but now there is another one. This is the error I get:

 

Fatal error: Call to a member function on a non-object in /home/mcclella/public_html/booktest/results.php on line 37

 

This is line 37:

 $row = $result->fetch_assoc();

 

The rest of the code:

 

<?php
  // create short variable names
  $searchtype=$_POST['searchtype'];
  $searchterm=$_POST['searchterm'];

  $searchterm= trim($searchterm);

  if (!$searchtype || !$searchterm)
  {
    die("You have not entered search details.  Please go back and try again.");
  }
  
  if (!get_magic_quotes_gpc())
  {
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
  }

 $db=mysql_connect ("localhost", "xxxxxx_booktst", "xxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
      mysql_select_db ("xxxxxx_bookorama");

  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = mysql_query($query) or die('Error, search query failed'); 

    $num_results = mysql_num_rows($result);

  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>';
  }


mysql_close();  

?>

 

Thanks for any help!

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.