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

Reply

Join Date: Aug 2006
Posts: 58
Reputation: desiguru is an unknown quantity at this point 
Solved Threads: 1
desiguru desiguru is offline Offline
Junior Poster in Training

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

 
0
  #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 7:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 166
Reputation: Lafinboy is an unknown quantity at this point 
Solved Threads: 7
Lafinboy's Avatar
Lafinboy Lafinboy is offline Offline
Junior Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Aug 2006
Posts: 58
Reputation: desiguru is an unknown quantity at this point 
Solved Threads: 1
desiguru desiguru is offline Offline
Junior Poster in Training

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

 
0
  #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 Thumbnails
image1.png   image2.png   image copy.jpg  
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 166
Reputation: Lafinboy is an unknown quantity at this point 
Solved Threads: 7
Lafinboy's Avatar
Lafinboy Lafinboy is offline Offline
Junior Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Aug 2006
Posts: 58
Reputation: desiguru is an unknown quantity at this point 
Solved Threads: 1
desiguru desiguru is offline Offline
Junior Poster in Training

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

 
0
  #5
Aug 3rd, 2006
All of the three.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 798
Reputation: pritaeas is on a distinguished road 
Solved Threads: 130
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Master Poster

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

 
0
  #6
Aug 6th, 2006
You can use

SELECT * FROM tablename WHERE CONCAT(firstname,lastname,phonenr) LIKE '%$search%'
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 27
Reputation: EFEXConsulting is an unknown quantity at this point 
Solved Threads: 2
EFEXConsulting EFEXConsulting is offline Offline
Light Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Aug 2006
Posts: 58
Reputation: desiguru is an unknown quantity at this point 
Solved Threads: 1
desiguru desiguru is offline Offline
Junior Poster in Training

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

 
0
  #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 Quick reply to this message  
Join Date: Apr 2005
Posts: 5
Reputation: Dean C is an unknown quantity at this point 
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..

 
0
  #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 Quick reply to this message  
Join Date: Nov 2006
Posts: 1
Reputation: mkhalid is an unknown quantity at this point 
Solved Threads: 0
mkhalid mkhalid is offline Offline
Newbie Poster

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

 
0
  #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
  1. SELECT rowid, (match (firstname, lastname, phone) against ('$keyword')) as score
  2. FROM contact
  3. where (match(firstname, lastname, phone) against ('$keywork'))
OR

  1. SELECT rowid, (match (firstname, lastname, phone) against ('$keyword')) as score
  2. FROM contact
  3. 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 3:00 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC