无法在新的PHP页面中显示搜索结果

时间:2014-12-23 12:28:38

标签: php mysql mysqli

我在将搜索结果显示到另一个php页面时遇到问题。

下面是我的第二个php页面的PHP代码:

if (isset($_GET['bookTitle'])) {
    $bookTitle = $_GET['bookTitle'];
    $sqlSearch = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName,  location, catDesc, bookPrice FROM nbc_book
    LEFT JOIN nbc_category ON nbc_book.catID = nbc_category.catID
    LEFT JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID LIKE CONCAT('%', bookTitle, '%') 
    WHERE bookTitle = " . $_GET['booktitle'] ;
    echo "string";
    $resultSearch = mysqli_query($conn,$sqlSearch) or die (mysql_error());

    while ($row = mysqli_fetch_assoc($resultSearch)){
        $category = $row['catDesc'];
        $bookTitle = $row['bookTitle'];
        $publisherName = $row['pubName'];
        $bookYear = $row['bookYear'];
        $bookPrice = $row['bookPrice'];
        echo "Result found!";
    }
}else{

    echo "Result not found!";
}

mysqli_free_result($resultSearch);
mysqli_close($conn);

这是我得到的错误:

Result not found!
Notice: Undefined variable: querySearch in /

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in /

任何人都可以帮我解决我的问题。如果有人能帮助我解决这个问题,我会很高兴。

1 个答案:

答案 0 :(得分:-1)

  

WHERE bookTitle =“。$ _GET ['booktitle'];

你错过了用引号'包装价值:

 WHERE bookTitle = '" . $_GET['booktitle'] . "' " ;

更新

  

为什么我收到如上所示的错误消息?

因为除了打开它之外你试图关闭sql连接!

我在brasked中移动了mysqli_free_result($resultSearch);mysqli_close($conn);

if (isset($_GET['bookTitle'])) {
  $bookTitle = $_GET['bookTitle'];
  $sqlSearch = "SELECT bookISBN, bookTitle, bookYear, nbc_book.catID, nbc_book.pubID, pubName,  location, catDesc, bookPrice FROM nbc_book
LEFT JOIN nbc_category ON nbc_book.catID = nbc_category.catID
LEFT JOIN nbc_publisher ON nbc_book.pubID = nbc_publisher.pubID LIKE CONCAT('%', bookTitle, '%') 
WHERE bookTitle = " . $_GET['booktitle'] ;
  echo "string";
  $resultSearch = mysqli_query($conn,$sqlSearch) or die (mysql_error());

  while ($row = mysqli_fetch_assoc($resultSearch)){
    $category = $row['catDesc'];
    $bookTitle = $row['bookTitle'];
    $publisherName = $row['pubName'];
    $bookYear = $row['bookYear'];
    $bookPrice = $row['bookPrice'];
    echo "Result found!";

  mysqli_free_result($resultSearch);
  mysqli_close($conn);
   }
}else{ 
   echo "Result not found!";
}

似乎未提供$_GET['booktitle'],但在您的代码中,您尝试释放结果并且不关闭任何内容!