Forum Moderators: coopster

Message Too Old, No Replies

query not returning expected fields

query not returning expected fields

         

scaleypate

8:49 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



I am trying to run a query within a query and cannot get the results that I am looking for. For some reasons, the second query will only pull one (personid) of the two columns that I am asking for (personid and phonenumber). Thanks in advance for your help!

$query = "Select * from contactsitejoin join people on people.personid = contactsitejoin.personid where siteid = $siteid";
$query_result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($query_result);
while($row = mysql_fetch_array($query_result)) {
print "<p><strong>{$row['position']}</strong> {$row['title']}{$row['fname']} {$row['lname']}; ";

To this point, the code works fine. It is returning the expected results.

$query2 = "Select * from personphonejoin where personid = {$row['personid']}";
$query_result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_array($query_result2);
while($row2 = mysql_fetch_array($query_result2)) {
print "Phone: {$row2['phonenumber']} (";

This is where the problem occurs. Nothing is being returned. So far, I have:

1. echoed query2. It is returning the correct query (select * from personphonejoin where personid = 2)

2. printed the results of the query. It is only returning the person id and not the phone number - that is the whole problem!

3. Checked spelling in the db and on the screen - everything matches.

4. Changed select * to select personid, phonenumber - no result.

Everything below here I have not tested because I cannot get beyond query2.

I am using the latest versions of php and mysql. The data types for personid is smallint[6] unsigned for both the phone and personphonejoin tables.

$query3 = "Select * from phone where phonenumber = {$row2['phonenumber']}";
$query_result3 = mysql_query($query3) or die(mysql_error());
$row3 = mysql_fetch_array($query_result);
while ($row3 = mysql_fetch_array($query_result3)) {
print "{$row3['phoneclass']} ";
}
}
print "</p>";

}

anshul

7:34 am on Jun 25, 2005 (gmt 0)

10+ Year Member



Remove extra lines everywhere:
$row = mysql_fetch_array($query_result);
OR
$row2 = mysql_fetch_array($query_result2);
OR
$row3 = mysql_fetch_array($query_result3);

You're already & correctly using that is while loop.