You aren't going at this in a good way. There is no reason to have a separate query. Get all the data you need in one query. I'm guessing that for each applicant matching your criteria you want to find all other applicants with the same ip. If so
SELECT ..., apps.applicant, ..., apps.ip, ..., ip_related.applicant AS ipr_applicant FROM apps
FROM apps
LEFT JOIN apps AS ip_related ON ip_related.ip = apps.ip AND ip_related.id != apps.id
WHERE ...
ORDER BY apps.id DESC";
Then, as you foreach over the result, just keep track on what applicant you are handling
$last_app = null;
$ipr_apps = null;
while($nt = mysql_fetch_array()) {
// just started on the next applicant.
if ($last_app != $nt['id']) {
// This corresponds to what happened in your former outermost loop
// You also need to
if ($ipr_apps) {
$altlist = implode(', ' . $ipr_apps);
// etc
}
$ipr_apps = array();
$last_app = $nt['id'];
}
// this corresponds to the results of your nested query
$ipr_apps[] = $nt['ipr_applicant'];
}
To help out while debugging, I'd start by turning on all error reporting. Furthermore, it's often helpful to see what a query really looks like, so
$alts_query = "SELECT apps.applicant FROM apps WHERE apps.ip = '$nt[ip]'";
echo $alts_query;
$alts_result = mysql_query($alts_query) OR die('FAIL');
And you most definitely want to show yourself any actual error. While 'FAIL' may accurately describe the situation, it is somewhat lacking in precision.
$alts_result = mysql_query($alts_query);
if (!$alts_result) {
// Now you have a whole lot better information to go by...
error_log(mysql_errno() . ': ' . mysql_error());
die('FAIL');
}
When the above is ok, why not try some more output
print_r($altlist);
And the actual problem in your code is that you don't go over all your related applicants, but only use the first...
$altlist = array();
while($alt_row = mysql_fetch_array($alts_results);
$altlist[] = $alt_row['applicant'];
$alts = implode(",", $altlist);