简体   繁体   English

用PHP搜索MySQL数据库

[英]Search MySQL database in PHP

I'm attempting to make a search page that allows a user to type any part of a title of a book from a mysql database. 我正在尝试创建一个搜索页面,允许用户从mysql数据库中键入书名的任何部分。 With this code I am getting no results no matter the word I put in. The goal is to use the query result and use num_rows to determine the number of rows in the result and the fetch_assoc method to fetch a result row as an associative array. 使用这段代码,无论我输入的是什么,我都没有得到任何结果。目标是使用查询结果并使用num_rows来确定结果中的行数,并使用fetch_assoc方法将结果行作为关联数组获取。 I am getting no errors. 我没有错。 Just blank results. 只是空白的结果。

The search books page: 搜索书页:

$page_title = "Search book";

include ('includes/header.php');
?>
<h2>Search Books by Title</h2>
<p>Enter one or more words in book title.</p>
<form action="searchbooksresults.php" method="get">
<input type="text" name="terms" size="40" required />&nbsp;&nbsp;
<input type="submit" name="Submit" id="Submit" value="Search Book" />
</form>
<?php

include ('includes/footer.php');
?>

The search results page 搜索结果页面

$page_title = "Books in Our Store";
require 'includes/header.php';
require_once('includes/database.php');

//retrieve the search terms from a query string
$term_string = filter_input(INPUT_GET, "term", FILTER_SANITIZE_STRING);
$terms = explode(" ", $term_string);

//sql query statement
$sql = "SELECT * FROM books WHERE 1";
foreach ($terms as $term){
$sql .= "AND CONCAT(title) LIKE %" .$term. "%";
}
//execute the query
$query = $conn->query($sql);

//using the query results to dertmine number of rows
$results = @$conn->num_rows($query);


//Handle errors
if (!$query) {
$errno = $conn->errno;
$error = $conn->error;
$conn->close();
die("Selection failed: ($errno) $error.");
}
?>

<h2>Books: <?php $term ?></h2>
<table id="booklist" class="booklist">
    <tr>
    <th>Title</th>
    <th class="col2">Author</th>
    <th class="col3">Category</th>
    <th class="col4">Price</th>
    </tr>

<?php
while ($row = $query->fetch_assoc($results)) {
    echo "<tr>";
    echo "<td><a href='bookdetails.php?id=", $row['id'], "'>",     $row['title'], "</a></td>";
    echo "<td>", $row['author'], "</td>";
    echo "<td>", $row['category_id'], "</td>";
    echo "<td>", $row['price'], "</td>";
    echo "</tr>";
}
?>
</table>

<?php
require 'includes/footer.php';

Try replacing AND to OR because in the way you are using it right now a row must have all terms from input delimiter space. 尝试将AND替换为OR因为在您正在使用它的方式中,行必须具有来自输入分隔符空间的所有术语。 Also add single quote. 还要添加单引号。

$sql .= "AND CONCAT(title) LIKE %" .$term. "%";

To: 至:

$term_string = 'Term1 Term2 Term3';
$terms = explode(" ", preg_replace('/(\s)+/', ' ', trim($term_string))); //Remove multiple spaces

$sql = "SELECT * FROM books";
$temp = array();

foreach ($terms as $term){
    $temp[] = "title LIKE '%$term%'";
}
if (!empty($temp)) {
    $sql .= " WHERE ".implode(' OR ', $temp);
}

echo $sql;

Result: 结果:

SELECT * FROM books WHERE title LIKE '%Term1%' OR title LIKE '%Term2%' OR title LIKE '%Term3%'

Furthermore don't supress errors using @ . 此外,不要使用@抑制错误。 Finally try to use prepared statements. 最后尝试使用预处理语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM