| | |
it's simple but yet complicated...php and mysql search..
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2006
Posts: 58
Reputation:
Solved Threads: 1
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.
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 ::
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:
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: 58
Reputation:
Solved Threads: 1
•
•
•
•
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:
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
PHP Syntax (Toggle Plain Text)
SELECT rowid, (match (firstname, lastname, phone) against ('$keyword')) as score FROM contact where (match(firstname, lastname, phone) against ('$keywork'))
PHP Syntax (Toggle Plain Text)
SELECT rowid, (match (firstname, lastname, phone) against ('$keyword')) as score FROM contact where (match(firstname, lastname, phone) against ('$keywork') IN BOOLEAN MODE)
http://dev.mysql.com/doc/refman/4.1/...xt-search.html
Last edited by mkhalid; Nov 28th, 2006 at 3:00 am.
![]() |
Similar Threads
- 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
| Thread Tools | Search this Thread |
ajax apache api array beginner binary body broken cakephp checkbox class cms code cookies cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert interactive ip javascript job joomla js limit link login mail mediawiki menu mlm mobile msqli_multi_query multiple mycodeisbad mysql navigation oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion regex remote script search server sessions sms source space sql stored subdomain syntax system table tutorial unicode update upload url validator variable video web webapplications websitecontactform xml youtube





