kzs
kzs

Reputation: 1109

cannot retrieve data from mysql database

I have been following some php-mysql tutorial from a textbook. I should be able to retrieve some data from the "books" database, for example if I select author as searchtype and Michael as searchterm I should get some results since the author name is in the database. But, I am not getting any result after I submit the data from form. It just shows following:

Search Results 

Number of books found:

following is my html code for the form:

<html>
  <head>
    <title> Catalog Search</title>
  </head>
  <body>
    <h1>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>
</html>

I have a database named books and a php script named results.php:

<html>
  <head>
    <title>Search Results</title>
  </head>
  <body>
    <h1>Search Results</h1>
    <?php
      // create short variable names
      $searchtype=$_POST["searchtype"];
      $searchterm=$_POST["searchterm"];
      if (!$searchtype || !$searchterm)
      {
        echo 'You have not entered search details.
         Please go back and try again.';
        exit;
      }

      $searchterm= trim($searchterm);
      $searchtype = addslashes($searchtype);
      $searchterm = addslashes($searchterm);

      // Create connection
      $con=mysqli_connect("localhost","myusername","mypassword","books");

      // Check connection
      if (mysqli_connect_errno($con))
      {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

      $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
      $result = mysql_query($query);
      $num_results = mysql_num_rows($result);
      echo '<p>Number of books found: '.$num_results.'</p>';
      for ($i=0; $i <$num_results; $i++)
      {
        $row = mysql_fetch_array($result);
        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>';
      }
    ?>
  </body>
</html>

What's wrong with the code? Thanks in advance.

Upvotes: 0

Views: 1614

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

Your connection uses mysqli extension, therefore your query has to use that as well and not the mysql_*

$result = mysql_query($query); // Wrong extension. Use mysqli

should be

$result = mysqli_query($con,$query);

Then all the subsequent database functions have to use mysqli_*, you cannot mix the two together.

Side note: use mysqli_real_escape_string instead of addslashes.

Upvotes: 1

Related Questions