简体   繁体   English

使用mysql的书籍数据库的php搜索脚本

[英]php search script for a book database using mysql

I have a database of about 3000 books and i want to be able to search my database for books either by titles, subjects or authors.. I've done my best trying to write a script using php and mysql but i still didn't get it right. 我有大约3000本书的数据库,我希望能够按标题,主题或作者的身份在数据库中搜索书籍。我已经尽力尝试使用php和mysql编写脚本,但是我仍然没有修正它。 can anyone assist please. 谁能帮忙。 Below is how far I've come with the script.. and an example of the table in my mysql database 以下是脚本附带的内容..以及mysql数据库中表格的示例

<?php

if (@$find == "")




// Otherwise we connect to our Database

mysql_connect("localhost", "root", "erhun") or die(mysql_error());


mysql_select_db("books") or die(mysql_error());


// We perform a bit of filtering
@$find = strtoupper($find);
@$find = strip_tags($find);
@$find = trim ($find);

@$search = strip_tags(stripslashes(mysql_real_escape_string(@$db, @$_POST["search"])));

//query the database
@$query = ("SELECT * FROM `project` WHERE (`author1` LIKE '%$search%' OR `main_title`               `LIKE '%$search%' OR `subj1` LIKE '%$search%')");  


//displaying the data
@$results=mysql_query(@$query) or die(mysql_error ());

if(mysql_num_rows(@$results) >= 1)

//here the table starts
echo "<table id='results'>";
while($row=mysql_fetch_array($results))


{
echo "<tr><td><img src='image1/".$row['image']."' height='100px' width='90px'></td><td   valign='top'>
<b><a href='details.php?id=".$row['book_id']."'>".$row['main_title']."<br/>
By: ".$row['author1']."</a></b><br/>
<b>Call no:</b> ".$row['call_no']."<br/>
<b>Type:</b> ".$row['item_type']."<br/>
<b>Year:</b> ".$row['publdate']."</td></tr>";
}
echo "</table>";

?>

my table contains these different fields 我的表包含这些不同的字段

Full texts 全文
book_id image main_title author1 call_no item_type publdate publplace publshr item_home item_status subj1 subj2 book_id图片main_title author1 call_no item_type publdate publplace publshr item_home item_status subj1 subj2

Try like 尝试像

@$query = "SELECT * FROM `project` WHERE (`author1` LIKE '%$search%' OR `main_title` LIKE '%$search%' OR `subj1` LIKE '%$search%')"; 

You have an extra quote ` after "main_title" 您在“ main_title”之后加了一个引号`

UPDATED : 更新 :

Try changing this line : 尝试更改此行:

@$search = strip_tags(stripslashes(mysql_real_escape_string($db, @$_POST["search"])));

to : 至 :

@$search = strip_tags(stripslashes(mysql_real_escape_string( @$_POST["search"])));

The only thing I needed to change was dropping the $db variable, just passing in the search string, and it seems to work fine. 我唯一需要更改的就是删除$ db变量,只是传入搜索字符串,它似乎可以正常工作。 It might be that $db was needed for some other reason so I recommend going back to the documentation you got that from and investigate further. 可能由于其他一些原因而需要$ db,因此我建议返回您从中获得的文档并进行进一步调查。

PS I'd still recommend using MATCH AGAINST . PS我仍然建议使用MATCH AGAINST If your subject, title and author fields have an appropriate FULLTEXT index you can use MATCH AGAINST eg : 如果您的主题,标题和作者字段具有适当的FULLTEXT索引,则可以使用MATCH AGAINST,例如:

@$query = "SELECT * FROM `project` WHERE MATCH (subj1, main_title, author) AGAINST ('$search' IN NATURAL LANGUAGE MODE)";

This query would require that you have a fulltext index over all three columns: 此查询将要求您在所有三列上都有全文索引:

ALTER TABLE project ADD FULLTEXT(subj1, main_title, author);

The list of columns in the index must match the list of columns you want to MATCH AGAINST in the query for the sake of optimisation I believe. 为了优化,我相信索引中的列列表必须与您要在查询中匹配的列列表匹配。 You'd have to create other indexes if you wanted to be able to match against individual columns separately. 如果要能够分别与各个列进行匹配,则必须创建其他索引。 That query also worked on my test page and it should better support normal search queries with a bit of experimentation. 该查询也可以在我的测试页上使用,并且应该通过一些实验更好地支持常规搜索查询。

PPS If you have a choice of DB I would recommend using something with more support for different types of text searching. PPS如果您选择数据库,我建议您使用对不同类型的文本搜索有更多支持的工具。 The only relational databases I'm really familiar with are PostgreSQL and MySQL and PostgreSQL has a lot better support for text searching. 只有关系数据库,我真正熟悉是PostgreSQL和MySQL和PostgreSQL拥有文本搜索了很多更好的支持。

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

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