Jump to content

Any idea why this join code won't work?


Bl4ckMaj1k

Recommended Posts

In a nutshell here is what I am wishing to do. The user clicks on a link. The link has an ID# embedded in it. For this example, we will assume that the Link ID (lid) = 4-1-1

 

Now I have 2 tables:

 

Table 1 Columns:

ID  |  contact_fname  contact_lname 

10              Bl4ck                  Maj1k

           

Table 2 Columns:

ID  contact_id  link_id

1            10            4-1-1

 

I need to display the contact's First and Last name if he is associated with the current link. The way I thought to track that was via table joins. I have attempted a code but it doesnt work. I basically want to make it so whenever Table 1 has a person with an ID equal to the Contact ID of table 2 AND whenever the Link ID in our URL (which I am capturing via a $_GET function and storing in a variable) is equal to the Link ID in our table with the associated contact in it, I want to then display the First Name and Last Name. Below is my failed attempt at this. Weird thing is, I have done this several times before. I don't see myself doing anything different but I must be because it simply doesn't work. Any help would be greatly appreciated...

 

<?php 
$link_id = $_GET['lid'];

$query = "SELECT table1.id, table1.contact_fname, table1.contact_lname, table2.contact_id, table2.link_id FROM table1, table2 WHERE table1.id = table2.contact_id AND table2.link_id = $link_id";

$sql = mysql_query($query);

while($row = mysql_fetch_array($sql)) {
$contact_fname = $row['contact_fname '];
$contact_lname = $row['contact_lname '];

echo '' . $contact_fname . ' ' . $contact_lname . '' ;
}

?>

 

Please let me know what I am doing wrong here! Thanks boys and girls  :-)

 

Bl4ck Maj1k

Link to comment
Share on other sites

I'd almost be willing to bet money that since you haven't quoted the variable in the query string, your link id is being evaluated as a mathematical expression rather than a string thereby causing the query to be " . . . AND table2.link_id = 2"

Link to comment
Share on other sites

I'd almost be willing to bet money that since you haven't quoted the variable in the query string, your link id is being evaluated as a mathematical expression rather than a string thereby causing the query to be " . . . AND table2.link_id = 2"

 

How do you guys notice those small things!!!! Thanks bro! That at least got me past the first hurdle. I will now attempt my full code to ensure everything is working properly before I mark this as 'Solved'. Thanks again though that has saved my day!!!

Link to comment
Share on other sites

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.