日期:2014-05-16  浏览次数:20466 次

PHP调用mysql函数出错
<html>
  <head>
    <title>Book-O-Rama Search Result</title>
  </head>
  <body>
    <h1>Book-O-Rama Search Results</h1>
    <?
      $searchtype = $_POST['searchtype'];
      $searchterm = trim($_POST['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 = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');
      if (mysqli_connect_error())
      {
        echo 'Error: Could not connect to database. Please try again later.';
        exit;
      }
      
      $db->select_db("books");
     
      //$query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; 
      $query = "select * from books where ". $searchtype."="."'$searchterm'" ; 
      //$query = "select * from books"; 
      echo "$query\n";
          
      $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++)
      { 
        /* 此段被注释的代码运行出错,错误在于$result->fetch_assoc();
        $result->fetch_assoc();
        echo "<p><strong>".($i+1).". Title: ";
        echo htmlspecialchars(stripslashes($row['title']));
        
        echo "</strong><br />Author: ";
        echo stripslashes($row['author']);
        
        echo "<br