简体   繁体   中英

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. can anyone assist please. Below is how far I've come with the script.. and an example of the table in my mysql database

<?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

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"

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. 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.

PS I'd still recommend using MATCH AGAINST . If your subject, title and author fields have an appropriate FULLTEXT index you can use MATCH AGAINST eg :

@$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. The only relational databases I'm really familiar with are PostgreSQL and MySQL and PostgreSQL has a lot better support for text searching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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