LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2005, 03:02 PM   #1
jamesjoy
LQ Newbie
 
Registered: Aug 2005
Location: Louth, Ireland
Distribution: FC 4
Posts: 27

Rep: Reputation: 15
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

Last edited by jamesjoy; 12-01-2005 at 03:03 PM.
 
Old 12-01-2005, 04:47 PM   #2
ricstirato
Member
 
Registered: Jan 2004
Location: Gießen, Germany
Distribution: Xubuntu 12.04, Mythbuntu, Ubuntu Server 12.04
Posts: 174

Rep: Reputation: 24
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.
 
Old 12-02-2005, 05:46 AM   #3
jamesjoy
LQ Newbie
 
Registered: Aug 2005
Location: Louth, Ireland
Distribution: FC 4
Posts: 27

Original Poster
Rep: Reputation: 15
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
 
Old 12-02-2005, 07:14 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-02-2005, 07:28 AM   #5
jamesjoy
LQ Newbie
 
Registered: Aug 2005
Location: Louth, Ireland
Distribution: FC 4
Posts: 27

Original Poster
Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot connect to mysql database from php rocordial Linux - Software 2 08-21-2005 02:05 PM
Looking for a documentation database template (PHP/mySQL) davee Linux - Software 1 05-11-2005 08:39 AM
PHP launches the entire database in one search?! Alexander.s Programming 5 04-28-2005 07:32 PM
php won't write in mysql database!! Boby Programming 5 04-26-2004 04:20 PM
MySQL php database and financial stuff esteeven Linux - Software 1 11-06-2003 03:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration