Bl4ckMaj1k Posted April 28, 2011 Share Posted April 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235033-any-idea-why-this-join-code-wont-work/ Share on other sites More sharing options...
Pikachu2000 Posted April 28, 2011 Share Posted April 28, 2011 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" Quote Link to comment https://forums.phpfreaks.com/topic/235033-any-idea-why-this-join-code-wont-work/#findComment-1207891 Share on other sites More sharing options...
Bl4ckMaj1k Posted April 28, 2011 Author Share Posted April 28, 2011 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/235033-any-idea-why-this-join-code-wont-work/#findComment-1207911 Share on other sites More sharing options...
Pikachu2000 Posted April 28, 2011 Share Posted April 28, 2011 We notice these things because we've been burned by them in the past, mostly. Quote Link to comment https://forums.phpfreaks.com/topic/235033-any-idea-why-this-join-code-wont-work/#findComment-1207917 Share on other sites More sharing options...
Bl4ckMaj1k Posted April 28, 2011 Author Share Posted April 28, 2011 Awesome!!! Next time I will definitely keep an eye out for that! This topic has officially been SOLVED!! My code works flawlessly with your help...thanks again mate!! ;-) Quote Link to comment https://forums.phpfreaks.com/topic/235033-any-idea-why-this-join-code-wont-work/#findComment-1207950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.