944,209 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 5330
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 3rd, 2006
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
desiguru is offline Offline
63 posts
since Aug 2006
Aug 3rd, 2006
0

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

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]
Reputation Points: 16
Solved Threads: 7
Junior Poster
Lafinboy is offline Offline
166 posts
since Jul 2004
Aug 3rd, 2006
0

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

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
Click image for larger version

Name:	image1.png
Views:	38
Size:	9.2 KB
ID:	2238   Click image for larger version

Name:	image2.png
Views:	33
Size:	5.3 KB
ID:	2239   Click image for larger version

Name:	image copy.jpg
Views:	200
Size:	57.6 KB
ID:	2240  
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
desiguru is offline Offline
63 posts
since Aug 2006
Aug 3rd, 2006
0

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

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?
Reputation Points: 16
Solved Threads: 7
Junior Poster
Lafinboy is offline Offline
166 posts
since Jul 2004
Aug 3rd, 2006
0

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

All of the three.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
desiguru is offline Offline
63 posts
since Aug 2006
Aug 6th, 2006
0

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

You can use

SELECT * FROM tablename WHERE CONCAT(firstname,lastname,phonenr) LIKE '%$search%'
Sponsor
Featured Poster
Reputation Points: 557
Solved Threads: 734
Bite my shiny metal ass!
pritaeas is offline Offline
4,207 posts
since Jul 2006
Aug 6th, 2006
0

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

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
Reputation Points: 10
Solved Threads: 2
Light Poster
EFEXConsulting is offline Offline
27 posts
since Aug 2006
Aug 6th, 2006
0

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

Quote 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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
desiguru is offline Offline
63 posts
since Aug 2006
Aug 7th, 2006
0

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

Do not use LafinBoy's code, it opens up a security risk. You should always sanitize your incoming post data.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dean C is offline Offline
5 posts
since Apr 2005
Nov 28th, 2006
0

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

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

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mkhalid is offline Offline
1 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: retrieving a particular value with a sql query
Next Thread in PHP Forum Timeline: Php & Xhtml





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC