LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Php search mysql database (https://www.linuxquestions.org/questions/programming-9/php-search-mysql-database-388386/)

jamesjoy 12-01-2005 03:02 PM

Php search mysql database
 
I am currently working on a site built in PHP and using mysql.

I am trying to integrate some kind of a seacrh bar into it that will allow a user do a complete search of my database.

For example, if i have a database with a list of books in it so,
title, author, catagory etc

how would i set up a search that would piuck a particular word out of those and display that entry.

I tried adding all the fields together into a big string like so

$text = $title . $ authour. $catagory etc

and then using in_array but had no luck

any suggestions would be great.

Thanks

ricstirato 12-01-2005 04:47 PM

Why not let MySQL do the work?

Use an SQL string like:

SELECT <your_fields_here> FROM table
WHERE '$search_string' IN (title, author, category)


Regards
ric.

jamesjoy 12-02-2005 05:46 AM

Thats brilliant thanks. Is there any way of gettin it to be less specific?

I mean that query only selectes the row if txt os identical to one of database enteries is there any king of command like

select * from table where dfsadaf,dsfaaffsda, contains $serach_string

graemef 12-02-2005 07:14 AM

Hi,

You will need to construct separate select statements for each search criteria which may sound like a lot of work, however that gives you an advantage in that with a bit of care you can add weights to each search allowing you to promote certain matches over others. For example a match on a title may be worth more than a match on a category. This is something that you can slowly work with and refine but if you have a lot of small sql queries then you will be able to progress down that path.

The first thing that you will need to do is combine the different searches together for example two queries may return the same row on a table. To keep a count of the number of times a row is duplicated you may want to use the php function array_intersect() and then you can combine two arrays using array_merge() and then remove duplicates with array_unique(). You can then sort it using asort() or probably arsort().

Have fun and learn a little more about SQL and PHP.

graeme.

jamesjoy 12-02-2005 07:28 AM

Sounds like this is going to be a long night!!!!!!!!!!!!!!!!

Thanks 4 the help might be posting back here to morrow when i get stuck!!!!

Thanks again


All times are GMT -5. The time now is 03:44 AM.