473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP and MySQL multiple table search query help

Hi guys and gals,

I am trying to do a search of a table in my DB but one of the fields is CompanyID which links to another table that has all the company information so I don't have to write the company details every time into our contacts DB.

However when I need to search all contacts (for example) if i need all customers in one company I do this SQL Query:

Expand|Select|Wrap|Line Numbers
  1. SELECT company.CompanyName,FirstName,LastName,CompanyID,CellPhone,OfficePhone,Email FROM persons,company WHERE persons.FirstName LIKE  '%tele%' || persons.LastName LIKE  '%tele%' ||  persons.Email LIKE '%tele%' OR company.CompanyName LIKE '%tele%' ORDER BY persons.FirstName
However as we all can see this is flawed due to the fact the system will duplicate info.

See code:
Expand|Select|Wrap|Line Numbers
  1. //RETURN RESULT
  2. $searchquery = $_POST['search'];
  3. $srch="%".$_POST['search']."%";
  4. //echo "search term: " . $srch; -- Testing
  5. $result = mysql_query ("SELECT company.CompanyName,FirstName,LastName,CompanyID,CellPhone,OfficePhone,Email FROM persons,company WHERE persons.FirstName LIKE  '$srch' || persons.LastName LIKE  '$srch' ||  persons.Email LIKE '$srch' OR company.CompanyName LIKE '$srch' ORDER BY persons.FirstName");
  6.  
  7. $total = mysql_num_rows($result);
  8.  
  9.  
  10. ?>
  11.  
  12. <h2><?php echo($total . " RECORD(S) FOUND CONTAINING: <font size=2 color='#CC0000'><b>" .$searchquery ); ?></b></font></div>
  13. <p>
  14.  
  15. <?php if($total>0) {
  16.  
  17. // print table header
  18. echo("<table cellspacing=\"0\" cellpadding=\"0\" width=\"500\" class=\"edittable\">
  19. <tr><th>Name / Company</th><th align='center'>Office No.</th><th align='center'>Mobile No.</th></tr>");
  20.  
  21. // fetch the current row into the array $row
  22. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
  23. {
  24. $compid = $row["CompanyID"];
  25. //I need to translat the compid above into a company name!
  26.  
  27. $name = $row["FirstName"] . " " . $row["LastName"] . "<br><strong>" . $compid ."</strong>";
  28. echo("<tr>");
  29. echo("<td><a href=\"contact.php?id=" . $row["ID"] . "&mode=read\">" . $name . "</a></td>");
  30. echo("<td align='center'>" . formatPhone($row["OfficePhone"]) . "</td>");
  31. echo("<td align='center'>" . formatPhone($row["CellPhone"]) . "</td>");
  32. //echo("<td align='center'>" . formatPhone($row["HomePhone"]) . "</td>");
  33. echo "</tr>";
  34. }
  35. echo("</table>");
  36. }
  37.  
  38.  
  39. mysql_close();
  40. ?>
All I need to be able to do is search the whole persons table for a specific search string! Including the company names! I just not sure how to make company.CompanyName replace persons.CompanyID when searching!

I know it's an easy one, I just haven't coded for ages and have lost the plot a little.

Cheers
Sep 6 '10 #1
1 4049
Found the answer by testing my instincts:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query ("SELECT persons.ID,CompanyName,FirstName,LastName,CompanyID,CellPhone,OfficePhone,Email FROM persons JOIN company ON persons.CompanyID = company.ID WHERE persons.FirstName LIKE  '$srch' || persons.LastName LIKE  '$srch' ||  persons.Email LIKE '$srch' || company.CompanyName LIKE '$srch' ORDER BY persons.FirstName");
I used the MySQL JOIN commant and joined the two table where the persons.CompanyID = company.ID Giving me the info I needed.

Anyone have another suggestion on how to do it??
Sep 6 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ralph Freshour | last post by:
I'm not sure the follow multiple table query is the right way to do what I need to do although it seems to be working: $php_SQL = "SELECT * ". "FROM basics, personal, photos ". "WHERE...
3
by: RG | last post by:
Hopefully someone here can help. I would like to perform multiple queries to a MySQL database and have all the results in 1 result set. Is this possible? TIA RG
1
by: Ray | last post by:
Hello all, I am attempting to delete multiple rows from multiple tables as follows: DELETE FROM attachments,responses,response_lines WHERE attachments.id IN(2,7,11) AND...
6
by: Matt Liverance | last post by:
I REALLY dont want to switch to oracle :( but I cant get these tables working any faster. I've got 2 dedicated servers, each with a slave, all run 32gig 15k rpm raid 5 on u320 perc raid...
1
by: SC | last post by:
I'm developing a site that may eventually have a very large database of users (hopefully, but who knows). It will be a community website where users can search each other (think Friendster,...
1
by: almu | last post by:
Hi all, I need help to build a set of queries in PHP/MySQL for a multiple-parameter search form, whereby the user will be able to search any number of ways (upto seven) Thanks
2
by: fusonts | last post by:
Can someon show me how to wirte a mySQL search query to search from only the open records. This is what I have but it continues to return records that are closed so I'm obviously doing something...
4
natalie99
by: natalie99 | last post by:
Hi All :) I am trying to stipulate rules for the return of a pricing value in Access. I know this should be very easy, I simply cannot seem to make my expressions work! The two tables have...
12
by: ziycon | last post by:
I have a basic search function working with PHP and mysql, can anyone help me to make a function that takes a single search criteria and searches multiple tables and gives the different results back...
2
by: SLauren | last post by:
Hi, I have a query where i have to filter some conditions. I am trying out for a query where i have to include the combinations (1,1) and (0,-1) but i need to exclude the combination (0,1). So...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.