473,396 Members | 1,734 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.

Problem with PHP query using multiple tables

chumlyumly
Hello -

Please help! I'm creating search pages using Dreamweaver 8, PHP, and MySQL database with Mac OS X. I think my code is good, but when I run my query, I come up with no data, even though I know there is data in the database that should be coming up.

I'm searching two tables that are linked with a foreign key (member_id). The members table is the main table (where "member_id" is an auto increment key) , and the specialty_groups table uses "member_id" to reference which member has what specialty.

I've created a LEFT JOIN with USING (member_id). I've tried modifying the code by hand several times, and still come up with "no data."

My code (as created by Dreamweaver 8) is below:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $varspecialty_rsmembersearchresultsm = "%";
  3. if (isset($_POST['mbrsearchspecialty'])) {
  4.   $varspecialty_rsmembersearchresultsm = $_POST['mbrsearchspecialty'];
  5. }
  6. $varfirstname_rsmembersearchresultsm = "%";
  7. if (isset($_POST['mbrsearchfname'])) {
  8.   $varfirstname_rsmembersearchresultsm = $_POST['mbrsearchfname'];
  9. }
  10. $varmbrlname_rsmembersearchresultsm = "%";
  11. if (isset($_POST['mbrsearchlname'])) {
  12.   $varmbrlname_rsmembersearchresultsm = $_POST['mbrsearchlname'];
  13. }
  14. $varmbrfirm_rsmembersearchresultsm = "%";
  15. if (isset($_POST['mbrsearchfirm'])) {
  16.   $varmbrfirm_rsmembersearchresultsm = $_POST['mbrsearchfirm'];
  17. }
  18. $varmedia_rsmembersearchresultsm = "%";
  19. if (isset($_POST['mbrsearchmedia'])) {
  20.   $varmedia_rsmembersearchresultsm = $_POST['mbrsearchmedia'];
  21. }
  22. $varfirstname_rsmembersearchresultsm = "%";
  23. if (isset($_POST['mbrsearchfname'])) {
  24.   $varfirstname_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchfname'] : addslashes($_POST['mbrsearchfname']);
  25. }
  26. $varmbrlname_rsmembersearchresultsm = "%";
  27. if (isset($_POST['mbrsearchlname'])) {
  28.   $varmbrlname_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchlname'] : addslashes($_POST['mbrsearchlname']);
  29. }
  30. $varmbrfirm_rsmembersearchresultsm = "%";
  31. if (isset($_POST['mbrsearchfirm'])) {
  32.   $varmbrfirm_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchfirm'] : addslashes($_POST['mbrsearchfirm']);
  33. }
  34. $varmedia_rsmembersearchresultsm = "%";
  35. if (isset($_POST['mbrsearchmedia'])) {
  36.   $varmedia_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchmedia'] : addslashes($_POST['mbrsearchmedia']);
  37. }
  38. $varspecialty_rsmembersearchresultsm = "%";
  39. if (isset($_POST['mbrsearchspecialty'])) {
  40.   $varspecialty_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchspecialty'] : addslashes($_POST['mbrsearchspecialty']);
  41. }
  42. mysql_select_db($database_connection, $connection);
  43. $query_rsmembersearchresultsm = sprintf("SELECT member.member_id, member.firstname, member.lastname, member.businessname, specialty_group.member_id FROM member LEFT JOIN specialty_group  USING (member_id) WHERE member.firstname LIKE '%%%s%%' AND member.lastname LIKE '%%%s%%' AND member.businessname LIKE '%%%s%%' AND member.media_contact = '%s' AND specialty_group.specialty = '%s'", $varfirstname_rsmembersearchresultsm,$varmbrlname_rsmembersearchresultsm,$varmbrfirm_rsmembersearchresultsm,$varmedia_rsmembersearchresultsm,$varspecialty_rsmembersearchresultsm);
  44. $rsmembersearchresultsm = mysql_query($query_rsmembersearchresultsm, $connection) or die(mysql_error());
  45. $row_rsmembersearchresultsm = mysql_fetch_assoc($rsmembersearchresultsm);
  46. $totalRows_rsmembersearchresultsm = mysql_num_rows($rsmembersearchresultsm);
  47. ?>
  48.  
Thank you in advance for taking the time to help.

Best,

Charity
Aug 15 '07 #1
9 2105
pbmods
5,821 Expert 4TB
Heya, Charity. Welcome to TSDN!

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `member`.`member_id`,
  3.         `member`.`firstname`,
  4.         `member`.`lastname`,
  5.         `member`.`businessname`,
  6.         `specialty_group`.`member_id`
  7.     FROM
  8.     (
  9.             `member`
  10.         LEFT JOIN
  11.             `specialty_group`
  12.                 USING
  13.                     (`member_id`)
  14.     )
  15.     WHERE
  16.     (
  17.             `member`.`firstname` LIKE '%%%s%%' -- valid - sprintf()
  18.         AND
  19.             `member`.`lastname` LIKE '%%%s%%'
  20.         AND
  21.             `member`.`businessname` LIKE '%%%s%%'
  22.         AND
  23.             `member`.`media_contact` = '%s'
  24.         AND
  25.             `specialty_group`.`specialty` = '%s'
  26.     )
  27.  
I see nothing immediately wrong with the structure of your query,

Try adding these lines right after you create the query:
Expand|Select|Wrap|Line Numbers
  1. $query_rsmembersearchresultsm = "SELECT ... ";
  2.  
  3. echo $query_rsmembersearchresultsm, '<br /><br />';
  4.  
Does the query look right, or are there any missing fields? Try dropping that query into your MySQL client app and see if it is getting results.
Aug 15 '07 #2
Hello and thank you for responding.

I copied the text below and entered it directly into my MySQL client admin and got the response "empty recordset."

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `member`.`member_id`,
  3.         `member`.`firstname`,
  4.         `member`.`lastname`,
  5.         `member`.`businessname`,
  6.         `specialty_group`.`member_id`
  7.     FROM
  8.     (
  9.             `member`
  10.         LEFT JOIN
  11.             `specialty_group`
  12.                 USING
  13.                     (`member_id`)
  14.     )
  15.     WHERE
  16.     (
  17.             `member`.`firstname` LIKE '%%%s%%' -- valid - sprintf()
  18.         AND
  19.             `member`.`lastname` LIKE '%%%s%%'
  20.         AND
  21.             `member`.`businessname` LIKE '%%%s%%'
  22.         AND
  23.             `member`.`media_contact` = '%s'
  24.         AND
  25.             `specialty_group`.`specialty` = '%s'
  26.     )
  27.  
I'm not sure where exactly I should put the code below:

Try adding these lines right after you create the query:
Expand|Select|Wrap|Line Numbers
  1. $query_rsmembersearchresultsm = "SELECT ... ";
  2.  
  3. echo $query_rsmembersearchresultsm, '<br /><br />';
  4.  
Should I put the code into my .php page after the code I already have, or elsewhere? Sorry - I'm confused! When I put it in my page as it is now after the code I have, the top of the page (as posted) says, "SELECT ..."

Thanks again!

Charity
Aug 15 '07 #3
Hello - I'm back with an edit to my original post.

So I've gotten the query to work by replacing all "="s with "LIKE." However, I'm now running into another problem.

I'm using LEFT JOIN to join my 'members' table and my 'specialty_groups' table (USING member_id). This is supposed to return all rows from the first table ('members'), whether or not there's a match in the second table ('specialty_groups').

Instead, what I'm getting is only rows that are in the 'specialty_groups' table.

I've tried replacing LEFT JOIN with RIGHT JOIN to see if that makes a difference, and it doesn't. I still only get records that have a match on the 'specialty_groups' table.

My code is below:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $varspecialty_rsmembersearchresultsm = "%";
  3. if (isset($_POST['mbrsearchspecialty'])) {
  4.   $varspecialty_rsmembersearchresultsm = $_POST['mbrsearchspecialty'];
  5. }
  6. $varfirstname_rsmembersearchresultsm = "%";
  7. if (isset($_POST['mbrsearchfname'])) {
  8.   $varfirstname_rsmembersearchresultsm = $_POST['mbrsearchfname'];
  9. }
  10. $varmbrlname_rsmembersearchresultsm = "%";
  11. if (isset($_POST['mbrsearchlname'])) {
  12.   $varmbrlname_rsmembersearchresultsm = $_POST['mbrsearchlname'];
  13. }
  14. $varmbrfirm_rsmembersearchresultsm = "%";
  15. if (isset($_POST['mbrsearchfirm'])) {
  16.   $varmbrfirm_rsmembersearchresultsm = $_POST['mbrsearchfirm'];
  17. }
  18. $varmedia_rsmembersearchresultsm = "%";
  19. if (isset($_POST['mbrsearchmedia'])) {
  20.   $varmedia_rsmembersearchresultsm = $_POST['mbrsearchmedia'];
  21. }
  22. $varfirstname_rsmembersearchresultsm = "%";
  23. if (isset($_POST['mbrsearchfname'])) {
  24.   $varfirstname_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchfname'] : addslashes($_POST['mbrsearchfname']);
  25. }
  26. $varmbrlname_rsmembersearchresultsm = "%";
  27. if (isset($_POST['mbrsearchlname'])) {
  28.   $varmbrlname_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchlname'] : addslashes($_POST['mbrsearchlname']);
  29. }
  30. $varmbrfirm_rsmembersearchresultsm = "%";
  31. if (isset($_POST['mbrsearchfirm'])) {
  32.   $varmbrfirm_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchfirm'] : addslashes($_POST['mbrsearchfirm']);
  33. }
  34. $varmedia_rsmembersearchresultsm = "%";
  35. if (isset($_POST['mbrsearchmedia'])) {
  36.   $varmedia_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchmedia'] : addslashes($_POST['mbrsearchmedia']);
  37. }
  38. $varspecialty_rsmembersearchresultsm = "%";
  39. if (isset($_POST['mbrsearchspecialty'])) {
  40.   $varspecialty_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchspecialty'] : addslashes($_POST['mbrsearchspecialty']);
  41. }
  42. mysql_select_db($database_connection, $connection);
  43. $query_rsmembersearchresultsm = sprintf("SELECT member.member_id, member.firstname, member.lastname, member.businessname, specialty_group.member_id FROM member LEFT JOIN specialty_group  ON member.member_id = specialty_group.member_id WHERE member.firstname LIKE '%%%s%%' AND member.lastname LIKE '%%%s%%' AND member.businessname LIKE '%%%s%%' AND member.media_contact LIKE '%%%s%%' AND specialty_group.specialty LIKE '%%%s%%' GROUP BY member.member_id", $varfirstname_rsmembersearchresultsm,$varmbrlname_rsmembersearchresultsm,$varmbrfirm_rsmembersearchresultsm,$varmedia_rsmembersearchresultsm,$varspecialty_rsmembersearchresultsm);
  44. $rsmembersearchresultsm = mysql_query($query_rsmembersearchresultsm, $connection) or die(mysql_error());
  45. $row_rsmembersearchresultsm = mysql_fetch_assoc($rsmembersearchresultsm);
  46. $totalRows_rsmembersearchresultsm = mysql_num_rows($rsmembersearchresultsm);
  47.  ?>
  48.  
Any ideas? I'd be ever so grateful.

Thanks! Charity
Aug 18 '07 #4
pbmods
5,821 Expert 4TB
Heya, Charity.

You have this in your WHERE clause:
Expand|Select|Wrap|Line Numbers
  1. specialty_group.specialty LIKE '%%%s%%'
  2.  
Which will automatically reject any rows where 'specialty_group'.'specialty' is null.

To fix this, you might want to change your query slightly:
Expand|Select|Wrap|Line Numbers
  1. (specialty_group.specialty LIKE '%%%s%%' OR specialty_group.specialty IS NULL)
  2.  
Aug 18 '07 #5
Thank you!!

You totally rock. That solved it.

Thank you thank you thank you.

Now another question - completely related. This query is on a page that lists the search results as the member's first name, last name, and business name. People can then click on the member's first name to see their entire record (address, phone numbers, etc.).

I'm running the query from two tables - the parent table is the 'member' table, and the child table is the 'specialty_groups' table.

Whenever I run a search for a member that has specialties (which are a search parameter and are housed in the specialty_groups table), their entire record comes up. That's great.

However, when I search for a member that doesn't have any specialties in the specialty_groups table, their member_id (i.e. the 'recordID') is not passed on to the page that gives all of that member's details. The link looks like this:

http://www.aialosangeles.org/databas....php?recordID=

The new code as I have it is now this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $varspecialty_rsmembersearchresultsm = "%";
  3. if (isset($_POST['mbrsearchspecialty'])) {
  4.   $varspecialty_rsmembersearchresultsm = $_POST['mbrsearchspecialty'];
  5. }
  6. $varfirstname_rsmembersearchresultsm = "%";
  7. if (isset($_POST['mbrsearchfname'])) {
  8.   $varfirstname_rsmembersearchresultsm = $_POST['mbrsearchfname'];
  9. }
  10. $varmbrlname_rsmembersearchresultsm = "%";
  11. if (isset($_POST['mbrsearchlname'])) {
  12.   $varmbrlname_rsmembersearchresultsm = $_POST['mbrsearchlname'];
  13. }
  14. $varmbrfirm_rsmembersearchresultsm = "%";
  15. if (isset($_POST['mbrsearchfirm'])) {
  16.   $varmbrfirm_rsmembersearchresultsm = $_POST['mbrsearchfirm'];
  17. }
  18. $varmedia_rsmembersearchresultsm = "%";
  19. if (isset($_POST['mbrsearchmedia'])) {
  20.   $varmedia_rsmembersearchresultsm = $_POST['mbrsearchmedia'];
  21. }
  22. $varfirstname_rsmembersearchresultsm = "%";
  23. if (isset($_POST['mbrsearchfname'])) {
  24.   $varfirstname_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchfname'] : addslashes($_POST['mbrsearchfname']);
  25. }
  26. $varmbrlname_rsmembersearchresultsm = "%";
  27. if (isset($_POST['mbrsearchlname'])) {
  28.   $varmbrlname_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchlname'] : addslashes($_POST['mbrsearchlname']);
  29. }
  30. $varmbrfirm_rsmembersearchresultsm = "%";
  31. if (isset($_POST['mbrsearchfirm'])) {
  32.   $varmbrfirm_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchfirm'] : addslashes($_POST['mbrsearchfirm']);
  33. }
  34. $varmedia_rsmembersearchresultsm = "%";
  35. if (isset($_POST['mbrsearchmedia'])) {
  36.   $varmedia_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchmedia'] : addslashes($_POST['mbrsearchmedia']);
  37. }
  38. $varspecialty_rsmembersearchresultsm = "%";
  39. if (isset($_POST['mbrsearchspecialty'])) {
  40.   $varspecialty_rsmembersearchresultsm = (get_magic_quotes_gpc()) ? $_POST['mbrsearchspecialty'] : addslashes($_POST['mbrsearchspecialty']);
  41. }
  42. mysql_select_db($database_connection, $connection);
  43. $query_rsmembersearchresultsm = sprintf("SELECT member.member_id, member.firstname, member.lastname, member.businessname, specialty_group.member_id FROM member LEFT JOIN specialty_group  ON member.member_id = specialty_group.member_id WHERE member.firstname LIKE '%%%s%%' AND member.lastname LIKE '%%%s%%' AND member.businessname LIKE '%%%s%%' AND (member.media_contact LIKE '%%%s%%' OR member.media_contact IS NULL) AND (specialty_group.specialty LIKE '%%%s%%' OR specialty_group.specialty IS NULL) GROUP BY member.member_id", $varfirstname_rsmembersearchresultsm,$varmbrlname_rsmembersearchresultsm,$varmbrfirm_rsmembersearchresultsm,$varmedia_rsmembersearchresultsm,$varspecialty_rsmembersearchresultsm);
  44. $rsmembersearchresultsm = mysql_query($query_rsmembersearchresultsm, $connection) or die(mysql_error());
  45. $row_rsmembersearchresultsm = mysql_fetch_assoc($rsmembersearchresultsm);
  46. $totalRows_rsmembersearchresultsm = mysql_num_rows($rsmembersearchresultsm);
  47.  ?>
  48.  
Any ideas or suggestions are appreciated!
Aug 18 '07 #6
pbmods
5,821 Expert 4TB
Heya, Charity.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 18 '07 #7
An addendum to my previous edited post:

When I create my query in my recordset in Dreamweaver and run a test, records are shown; however, under the columns 'member.member_id' and 'specialty_group.member_id', there is no information...unless the record has rows in the 'specialty_group' table.

If the person doesn't have any specialties listed in the 'specialty_group' table, then their member.member_id doesn't show up!

Why, why, why??

Thanks for your help!
Aug 19 '07 #8
pbmods
5,821 Expert 4TB
Heya, Charity.

What does your query look like now?
Aug 19 '07 #9
If the person doesn't have any specialties listed in the 'specialty_group' table, then their member.member_id doesn't show up!
I am facing the similar issue, did you find out the solution for this...???
Sep 22 '07 #10

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

Similar topics

3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
2
by: Josh Felker | last post by:
Hey everyone. I have a daily log form with my investing info. In that form I have 2 subforms, the first shows my profits on all the stocks I traded that day, the second shows my profits on...
39
by: Marcin Zmyslowski | last post by:
Hello all! I have the following problem with MS Access 2003 permissions. I have two users. One is admin and the second one is user who has full permissions to enter modify and read data. I...
5
by: mimo | last post by:
Hello, I have seen samples on how to pull data from one table and save back to it using the Form View control. How do I pull from multiple tables and save back to multiple tables on one...
4
by: Raj | last post by:
Hi all, I have couple of questions some one plzz help 1.I have a query which run for 2 hours on my production machine, it returns 1.5 millon rows, i looked at the explain plan it is picking up...
1
by: fong.yang | last post by:
I've got a database with about 300,000 records. There are several different tables that are set up the same way with identical fields. I have the same queries individually set up for each table. ...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
1
by: crazdandconfusd | last post by:
I have a database with two tables I use for shipping information. One is for if I'm only shipping one item and the other is for a back page of a report for when I ship multiple items. If I'm...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
0
by: The Frog | last post by:
Hello Everyone, I have been asked to try and create a single SQL query to retrieve product information from a database. The way that data is arranged is that in some tables there are user...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.