0

i have the following code

<?php
$id = $_SESSION["user_session"];
$get = mysql_query("SELECT * FROM table WHERE id='$id'");
$total_found = mysql_num_rows($get);
if($total_found==0){
	my code goes here
		}else{
			while($g = mysql_fetch_array($get)){
			$id_2 = $g["id"];			
			$query = mysql_query("SELECT * FROM table WHERE id != '$id' AND id!='$id_2'");
			while($z = mysql_fetch_array($query)){
				mycode goes here
			}
		}

?>

but it is not working.........!! please let me know..........how can i implent double loop............becuse the above code is fetching only on id from first loop...........!! how can i do that.........!!

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Atli
1

Shishtawitch,

The code looks like it should work, however. two things .....

1. At the line $id_2 = $g["id"]; , surely $id_2 must be equal to $id because it comes from a row resulting from a query which returns rows where id=='$id' . If I'm right, then the second query doesn't need AND id!='$id_2' because that condition is already covered by AND id!='$id' .

2. The second query will return no rows if table has just one row or if in all rows id==$id .

Airshow

0

Hey.

Just wanted to point out that number should not be quoted in MySQL queries. Can cause all sorts of problems.

Assuming your $id is in fact a number:

$query = mysql_query("SELECT * FROM table WHERE id != '$id' AND id!='$id_2'");
// Should be
$query = mysql_query("SELECT * FROM table WHERE id != $id AND id != $id_2");
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.