954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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....

desiguru
Junior Poster in Training
63 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 

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]

Lafinboy
Junior Poster
172 posts since Jul 2004
Reputation Points: 16
Solved Threads: 7
 

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!!!!!!!!!

Attachments image1.png 9.24KB image2.png 5.34KB image_copy.jpg 57.57KB
desiguru
Junior Poster in Training
63 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 

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?

Lafinboy
Junior Poster
172 posts since Jul 2004
Reputation Points: 16
Solved Threads: 7
 

All of the three.

desiguru
Junior Poster in Training
63 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 

You can use

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

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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

EFEXConsulting
Light Poster
27 posts since Aug 2006
Reputation Points: 10
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

I will try my best and if I have further questions, I will post them here. Thanks for your help :idea:

desiguru
Junior Poster in Training
63 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 

Do not use LafinBoy's code, it opens up a security risk. You should always sanitize your incoming post data.

Dean C
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
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

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/en/fulltext-search.html

mkhalid
Newbie Poster
1 post since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

i have a question, what if you had many search fields to search a table. for instance, if you had a table with many columns (that you would search through) like first name, last name, birth date, how would you search for all three queries and return only results that matched the user's input data?

for instance, if they wanted to search for all the people in my_search_table who had the first name 'Glen' AND the last name 'Lambert' AND who have a birthdate of 02/05/82. how can this be done in the query? something like
[php]
$result = mysql_query("SELECT first_name, last_name, birth_date
FROM my_search_table
WHERE first_name LIKE '%". flname ."%' AND last_name LIKE '%". $lname ."%' AND
birth_month LIKE '%". $birth_month ."%' AND
birth_day LIKE '%". $birth_day ."%' AND
birth_year LIKE '%". $birth_year ."%'")
or die(mysql_error());
[/php]
how would you have that effect?

boo_lolly
Light Poster
35 posts since Nov 2006
Reputation Points: 10
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....



this is the code:

if you type john all the j will appear on the search table...
please ym me or email me at [EMAIL="hazel_0821@yahoo.com"]hazel_0821@yahoo.com[/EMAIL] for more questions


<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("employees", $con);
$sql="INSERT INTO employee_data (emp_id,f_name,l_name,title,age,yos,salary,perks,email)
Values('$_POST[emp_id]','$_POST[f_name]','$_POST[l_name]','$_POST[title]','$_POST[age]','$_POST[yos]','$_POST[salary]','$_POST[perks]','$_POST[email]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>

hazel_0821
Newbie Poster
5 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Thanks everyone for your help. Everything is now working fine. I tried all of the codes. After playing around wit them they work perfect...

desiguru
Junior Poster in Training
63 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You