Jump to content

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          | james@test.com |      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 = 'james@test.com'";
$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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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