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
https://forums.phpfreaks.com/topic/235033-any-idea-why-this-join-code-wont-work/
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"

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!!!

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.