Jump to content

Nested Query


bdmovies

Recommended Posts

For some reason, this chunk of code is not looping through the way it should. When I break each query up and run in by itself it works fine, but I need it to loop through

 

<?php
# If the user is not an admin, only query specific items
if($_SESSION['accessLevel'] < "3") {
# Search CASES table for Client Reference
if($field == "clientReference") {
	$sql = "SELECT caseID FROM cases WHERE $field = '$query'";
	$result = $database->query($sql);
	while($caseID = $database->fetchAssoc($result)) {
		$sql = "SELECT addressID, serveeID FROM serviceJob WHERE caseID = '$caseID[caseID]'";
		$result = $database->query($sql);
		while($row = $database->fetchArray($result)) {
			$sql = "SELECT servees.serveeName, addresses.serviceAddress FROM addresses, servees WHERE serveeID = '$row[serveeID]'  AND addressID = '$row[addressID]'"; # THIS IS WHAT NEEDS TO BE LOOPING THROUGH EACH $row[serveeID] BUT IT ONLY RETURNS THE 1ST RESULT
			$result = $database->query($sql);
			while($row=$database->fetchAssoc($result)) {
				echo $row['serveeName'] . " at " . $row['serviceAddress'] . "<br/>";
			} # End display loop
		} # END $row loop
	} # End caseID loop
} # End the client Reference IF statement
} # End the access level condition
?>

Link to comment
https://forums.phpfreaks.com/topic/137738-nested-query/
Share on other sites

Ok, after some more digging, here is what I have found:

 

<?php
	while($caseID = $database->fetchAssoc($theResult)) {
		$sql = "SELECT addressID, serveeID FROM serviceJob WHERE caseID = '$caseID[caseID]'";
		$result = $database->query($sql);
		$nums = $database->numRows($result); // Returning 7
		$row = $database->fetchAssoc($result);
		$i = 1;
		while($i <= $nums) {
			echo $i . ". " .$row['addressID'] . " is the addressID. " . $row['serveeID'] . " is the serveeID<br/>"; // This is looping 7 times, but it isn't rolling through the array, it's the 1st result 7 times
			$i++;
		} # END $row loop

?>

Link to comment
https://forums.phpfreaks.com/topic/137738-nested-query/#findComment-719976
Share on other sites

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.