•
•
•
•
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
![]() |
| |
•
•
Join Date: Aug 2006
Posts: 31
Reputation:
Rep Power: 3
Solved Threads: 0
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.
•
•
Join Date: Jul 2004
Location: Sydney, Australia
Posts: 166
Reputation:
Rep Power: 5
Solved Threads: 7
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]
[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 ::
Lafinboy Productions
:: Website Design :: Website Development ::
•
•
Join Date: Jul 2004
Location: Sydney, Australia
Posts: 166
Reputation:
Rep Power: 5
Solved Threads: 7
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 ::
Lafinboy Productions
:: Website Design :: Website Development ::
•
•
Join Date: Aug 2006
Posts: 27
Reputation:
Rep Power: 3
Solved Threads: 2
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
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
ASP.NET/PHP web-development, graphics/web Design, site Promotion
•
•
Join Date: Aug 2006
Posts: 31
Reputation:
Rep Power: 3
Solved Threads: 0
•
•
•
•
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
•
•
Join Date: Nov 2006
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
OR
For detail about the full text search you can read
http://dev.mysql.com/doc/refman/4.1/...xt-search.html
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
adsense adult advertising blog blogging bomb book business coding competition copyright daniweb development earth engine environment failure forum gentoo google internet legal linux malware marketing mcafee microsoft monetization msn news operating pagerank phishing php privacy publishing revenue search security silverlight software spam spyware support system technical web webmaster wiki yahoo
- mysql search and display data help.... (MySQL)
- Making Search Engin using PHP & MySQL to search in DB (PHP)
- 250 MB Disk / 40 GB Traffic Free PHP & MySQL Host (Web Hosting Deals)
- configure PHP to work with Mysql (MySQL)
- How to hyperlink mysql search results? (MySQL)
- Mysql Search results (MySQL)
Other Threads in the PHP Forum
- Previous Thread: retrieving a particular value with a sql query
- Next Thread: Php & Xhtml


Hybrid Mode