DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   it's simple but yet complicated...php and mysql search.. (http://www.daniweb.com/forums/thread51710.html)

desiguru Aug 3rd, 2006 6:31 pm
it's simple but yet complicated...php and mysql search..
 
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....

Lafinboy Aug 3rd, 2006 7:39 pm
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]

desiguru Aug 3rd, 2006 8:31 pm
Re: it's simple but yet complicated...php and mysql search..
 
3 Attachment(s)
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!!!!!!!!!

Lafinboy Aug 3rd, 2006 8:46 pm
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?

desiguru Aug 3rd, 2006 9:45 pm
Re: it's simple but yet complicated...php and mysql search..
 
All of the three.

pritaeas Aug 6th, 2006 2:02 pm
Re: it's simple but yet complicated...php and mysql search..
 
You can use

SELECT * FROM tablename WHERE CONCAT(firstname,lastname,phonenr) LIKE '%$search%'

EFEXConsulting Aug 6th, 2006 8:15 pm
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

desiguru Aug 6th, 2006 8:57 pm
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 :idea:

Dean C Aug 7th, 2006 2:32 pm
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.

mkhalid Nov 28th, 2006 1:59 am
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
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


All times are GMT -4. The time now is 10:50 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC