...

View Full Version : Resolved Display data from another table help



d.fanning6388
05-06-2009, 08:54 PM
Struggling to get my head around this code, :confused:

I have two tables linked with a compound key table (applicants), the two tables I am using are job_index table and users table i am trying to get all the users who have 'applied' for job_id - 1.



$query = "SELECT * FROM applicants WHERE job_id='1'";
$result = mysql_query($query) or die ("Error in query");

if (mysql_num_rows($result)>0)
{
while ($row = @ mysql_fetch_array($result)) {
print $row["user_id"];
$user_id = $row["user_id"];

$query = "SELECT * FROM users WHERE id='$user_id'";
$result = mysql_query($query) or die ("Error in query");

if (mysql_num_rows($result)>0)
{
while ($row = @ mysql_fetch_array($result)) {

print "<b>" .$row["id"] . "</b><br/><br/>";
print "<b>" .$row["email"] . "</b><br/><br/>";
}}

}}


The result gets me three user_id's linked from the user table, but only ID - 1 is displayed



11

administrators

2

3


Any ideas?

venegal
05-06-2009, 09:08 PM
You are overwriting the $result of the first query with the second one there. Give it another name.

d.fanning6388
05-06-2009, 09:18 PM
Thanks for quick reply, i've amended the code to this, still causing an issue though.



$query = "SELECT * from applicants WHERE job_id = '1'";
$result = mysql_query($query) or die ("Error in query");

if (mysql_num_rows($result)>0)
{

while ($row = @ mysql_fetch_array($result)) {

print "<b>" .$row["user_id"] . "</b><br/><br/>";
$user_id = $row["user_id"];


$query_new = "SELECT * from users WHERE id = '".$user_id."'";
$result_new = mysql_query($query_new) or die ("Error in query");

if (mysql_num_rows($result_new)>0)
{
while ($row_new = @ mysql_fetch_array($result_new)) {
print "<b>" .$row_new["email"] . "</b><br/><br/>";
print "working<br/><br/>";
}}

}}


I've used an extra line of code to output 'working' in the 2nd loop, only seems to use the loop once even though its called 3 times in the 1st loop



1

administrators

working

2

3

venegal
05-06-2009, 09:30 PM
That seems to suggest, that those users in fact do not exist. Are you sure they are there?

d.fanning6388
05-06-2009, 09:40 PM
:eek:

Thanks, how stupid do I feel now, I had deleted the records on the table. :p

Didn't even think too look, I new it was something basic!!

Thanks Again!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum