User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 425,928 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,689 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 3176 | Replies: 12
Reply
Join Date: Aug 2006
Posts: 31
Reputation: desiguru is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
desiguru desiguru is offline Offline
Light Poster

Help it's simple but yet complicated...php and mysql search..

  #1  
Aug 3rd, 2006
I fond some tutorial that can search one field of the table in a database,. But I would like to search the whole table.. In my table I have people's name by first name, last name, phone number etc... how can I display the whole thing as a result? For example if someone was to type only John... the results would show, John's first name, last name, phone number... etc....
Last edited by desiguru : Aug 3rd, 2006 at 6:31 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Location: Sydney, Australia
Posts: 166
Reputation: Lafinboy is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 7
Lafinboy's Avatar
Lafinboy Lafinboy is offline Offline
Junior Poster

Re: it's simple but yet complicated...php and mysql search..

  #2  
Aug 3rd, 2006
You'll need to use a bit of conditional testing to build the appropriate sql string based on the submitted search terms. A simplified example shown below assumes the user can chooseto search on either firstname, lastname or both.

[PHP]$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$where = array();

if (isset( $fname ) && $fname!= "") {
$where[] = "(firstname LIKE '%$fname%')";
}
if (isset( $lname ) && $lname!= "") {
$where[] = "(lastname LIKE '%$lname%')";
}

$sql = ( "SELECT firstname, lastname, phonenr"
. "\nFROM tablename"
. (count( $where ) ? "\nWHERE " . implode( ' AND ', $where ) : "")
. "\nORDER BY lastname, firstname" );[/PHP]
If I've been a help please confirm by clicking the Add to Lafinboy's Reputation link in the header of this reply.

Lafinboy Productions
:: Website Design :: Website Development ::

Reply With Quote  
Join Date: Aug 2006
Posts: 31
Reputation: desiguru is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
desiguru desiguru is offline Offline
Light Poster

Help Re: it's simple but yet complicated...php and mysql search..

  #3  
Aug 3rd, 2006
I understand this concept but, I need only one search box.


I have attached some images, which will help you further to understand the concept...

Image 1 shows my tabel. Image 2 shows my table.... and the last image shows how I want to output...

And finally thanks very much for your help!!!!!!!!!
Attached Images
File Type: png image1.png (9.2 KB, 10 views)
File Type: png image2.png (5.3 KB, 10 views)
File Type: jpg image copy.jpg (57.6 KB, 9 views)
Reply With Quote  
Join Date: Jul 2004
Location: Sydney, Australia
Posts: 166
Reputation: Lafinboy is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 7
Lafinboy's Avatar
Lafinboy Lafinboy is offline Offline
Junior Poster

Re: it's simple but yet complicated...php and mysql search..

  #4  
Aug 3rd, 2006
What are the search parameters you want to handle? Does the single search box mean that users will only search on firstname, or possibly firstname and lastname, or possibly lastname only? Or all three?
If I've been a help please confirm by clicking the Add to Lafinboy's Reputation link in the header of this reply.

Lafinboy Productions
:: Website Design :: Website Development ::

Reply With Quote  
Join Date: Aug 2006
Posts: 31
Reputation: desiguru is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
desiguru desiguru is offline Offline
Light Poster

Re: it's simple but yet complicated...php and mysql search..

  #5  
Aug 3rd, 2006
All of the three.
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 207
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 25
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Posting Whiz in Training

Re: it's simple but yet complicated...php and mysql search..

  #6  
Aug 6th, 2006
You can use

SELECT * FROM tablename WHERE CONCAT(firstname,lastname,phonenr) LIKE '%$search%'
"Premature optimization is the root of all evil."
Donald Knuth / Tony Hoare
Reply With Quote  
Join Date: Aug 2006
Posts: 27
Reputation: EFEXConsulting is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
EFEXConsulting EFEXConsulting is offline Offline
Light Poster

Re: it's simple but yet complicated...php and mysql search..

  #7  
Aug 6th, 2006
btw, you should not play to hard with Like '%%', especially if you will have pager there (limit) as well as ability to sort by some fields (order by).
if you need - i can a short storty about doing that on big "for gamers" site.
briefly, try just to find some good book on database design, and read what can be done, what should be done and how

best regards
http://efex-consulting.com
ASP.NET/PHP web-development, graphics/web Design, site Promotion
Reply With Quote  
Join Date: Aug 2006
Posts: 31
Reputation: desiguru is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
desiguru desiguru is offline Offline
Light Poster

Re: it's simple but yet complicated...php and mysql search..

  #8  
Aug 6th, 2006
Originally Posted by EFEXConsulting
btw, you should not play to hard with Like '%%', especially if you will have pager there (limit) as well as ability to sort by some fields (order by).
if you need - i can a short storty about doing that on big "for gamers" site.
briefly, try just to find some good book on database design, and read what can be done, what should be done and how

best regards
I will try my best and if I have further questions, I will post them here. Thanks for your help
Reply With Quote  
Join Date: Apr 2005
Posts: 4
Reputation: Dean C is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Dean C's Avatar
Dean C Dean C is offline Offline
Newbie Poster

Re: it's simple but yet complicated...php and mysql search..

  #9  
Aug 7th, 2006
Do not use LafinBoy's code, it opens up a security risk. You should always sanitize your incoming post data.
Reply With Quote  
Join Date: Nov 2006
Posts: 1
Reputation: mkhalid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mkhalid mkhalid is offline Offline
Newbie Poster

Re: it's simple but yet complicated...php and mysql search..

  #10  
Nov 28th, 2006
In mysql, you can use "FULL TEXT SEARCH" option. For this you have to add index on all of three fields First name, Last name and Phone.
After that you will use the query
SELECT rowid, (match (firstname, lastname, phone) against ('$keyword')) as score 
FROM contact 
where (match(firstname, lastname, phone) against ('$keywork'))

OR

SELECT rowid, (match (firstname, lastname, phone) against ('$keyword')) as score 
FROM contact 
where (match(firstname, lastname, phone) against ('$keywork') IN BOOLEAN MODE)

For detail about the full text search you can read
http://dev.mysql.com/doc/refman/4.1/...xt-search.html
Last edited by mkhalid : Nov 28th, 2006 at 2:00 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC