php - comparing resultsets

Soldato
Joined
18 Oct 2002
Posts
9,044
Location
London
Having yet more php troubles (it never seems to end)...

Code:
while ($row = mysql_fetch_row($rs_full)) {
	while ($row2 = mysql_fetch_row($rs_selected)) {
		if ($row[0] == $row2[0]) {
			echo "$row2[1]";
		}	
	}
}

Should something like that work? I'm finding it doesn't, at all. Am I doing something wrong? It *looks* right, (to me at least)..

$rs_selected is a subset of $rs_full.
 
No ideas php gods? :eek:

I'll tell you what does happen, it cycles through all 30 of $rs_full.
But it will only print out the first result from $rs_selected and that's it. It never prints out the 2nd, 3rd, etc. Even though there is a few more results.

It'd be alright if I could see something wrong, I could then work out how to fix it, but I don't know what is wrong which makes it frustrating :o
 
Would it not be better to do this comparison within your MySQL query, and save you all the trouble and overhead of doing it with PHP?
Code:
SELECT from table1, table2 WHERE table1.row = table2.row
for example.
 
Sounds like an idea, but I'm not sure if it's possible with my queries:

Code:
$full_q = "SELECT CatID, CatName FROM category";
$rs_full = mysql_query($full_q);

$selected_q = "SELECT CatID, CatName
	FROM category
	WHERE CatID IN (SELECT CatID FROM product_category WHERE ProductID=".getInput($_GET['id']).")";
$rs_selected = mysql_query($selected_q);

I need the full listing of categories, but at the same time I need to know which of these categories are selected in the 'product_category' table.
 
KingAdora said:
Having yet more php troubles (it never seems to end)...

Code:
while ($row = mysql_fetch_row($rs_full)) {
	while ($row2 = mysql_fetch_row($rs_selected)) {
		if ($row[0] == $row2[0]) {
			echo "$row2[1]";
		}	
	}
}

Should something like that work? I'm finding it doesn't, at all. Am I doing something wrong? It *looks* right, (to me at least)..

$rs_selected is a subset of $rs_full.

You're not incrementing any counter, you're just statically comparing $row[0] against $row2[0]...

Try
Code:
$i = 0;
while ($row = mysql_fetch_row($rs_full)) {
$j = 0;
	while ($row2 = mysql_fetch_row($rs_selected)) {
		if ($row[$i] == $row2[$j]) {
			echo "$row2[$j+1]";
		}
		$j++;
	}
	$i++;
}
 
Back
Top Bottom