Jump to content

[SOLVED] Joining tables


rvdb86

Recommended Posts

Hi, i have the following code that joins two tables (tbl_customers and tbl_sites):

 

$query = "SELECT * FROM tbl_customers, tbl_sites ".
							"WHERE tbl_customers.customer_site = tbl_sites.site_id";

 

this selects all the records from the database. how would i modify the query to join only the fields where customer_site and site_id = 25 for example?

 

Thanks for any suggestions!

Link to comment
https://forums.phpfreaks.com/topic/145990-solved-joining-tables/
Share on other sites

tbl_customers:

 

customer_id SMALLINT (primary)

customer_name VARCHAR (50)

customer_email VARCHAR (100)

customer_site SMALLINT

 

tbl_sites:

 

site_id SMALLINT (primary)

site_name VARCHAR (50)

site_template VARCHAR (50)

 

|  customer_id  |  customer_name  |  customer_email  | customer_site  |

|        1        |        james          | [email protected] |      25          |

 

|  site_id  |  site_name  | site_template  |

|    25    |    mysite    |      sample      |

 

Hope this helps!

hey,

 

just tried that code, but again its not showing the results from the tbl_customers ???

 

i really appreciate the time you are taking to try and help me.

 

at the moment i use the following code that gets me the results:

 

$query = "SELECT * FROM tbl_customers WHERE customer_email = '[email protected]'";
$result = mysql_query($query)
   or die ("Couldn't get customer details");
   while ($row = mysql_fetch_array($result))
     {
      extract($row);

       query1 = "SELECT * FROM tbl_sites WHERE site_id = '$customer_site'";
       $result1 = mysql_query($query1)
            or die ("Couldn't get site details");
            while ($row1 = mysql_fetch_array($result1))
             {
                echo $customer_name;
                echo $site_template;
              }
       }

 

this works but i wasn't sure if it is the correct way to do things. maybe i was wrong and i will carry on like this or maybe there is a better solution?

I don't think it's the SQL that's the problem, I think it's how you are actually displaying it that's the problem.

 

while ($row1 = mysql_fetch_array($result1))

            {

                echo $customer_name;

                echo $site_template;

              }

 

You don't even use the $row1 array that you created there.  Are you familiar with how to actually display the data you are pulling out of the DB?

 

So the code you used when you tried my SQL.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.