Page 1 of 1

Two IF's or query WHERE + AND problem

Posted: Sat Nov 26, 2011 3:13 am
by unexperion
I have a problem with this one:
1. tableA (a_id, a_name, a_data)
2. tableB (b_id, b_name, a_id, a_name, b_data)
(all same columns have same data ; a_id=a_id, a_name=a_name in both tables!!!) listed on page1 as hrefs. The href leads to a result page2 with page2.php?id=$row['b_id'].
on page2 i use:

Code: Select all

 if (isset($_GET['id'])) {
	$id = intval($_GET['id']); {
with:

Code: Select all

  $query = "SELECT * FROM tableB WHERE b_id = $id";
That works. It displays ONE b_name that I need. However, I need another query result, that displays other "b_name" data BUT WITHOUT this one from the first query, and I need that data to be FILTERED with the same a_name, or same a_id.
I used:

Code: Select all

$query = "SELECT * FROM tableB WHERE b_id <> $id ";
But I get all other bnames listed. I need them filtered, but I don't seem to be able to do it correctly.
Can someone help me with proper second IF statement?
Something like this? (this isn't working :( )

Code: Select all

 if (isset($_GET['id'])) {
	$id = intval($_GET['id']); {
	$aid = intval($_GET['a_id']); {
//conn//
   $query = "SELECT * FROM tableB WHERE b_id <> $id AND b_id = $aid ";
   $sql = mysql_query($query)or die(mysql_error());
   while ($row = mysql_fetch_assoc($sql))
{
    echo $row['b_name']. "<br>";
}
}
}
}
Do I have to JOIN tableA maybe?
I tried :

Code: Select all

SELECT * FROM tableB JOIN tableA ON tableA.a_id=tableB.a_id WHERE tableB.b_id <> $id AND tableB.b_id = $aid";
and it's not working also...
p.s. do i have to close every { in one php or if I leave it open it will transcend to another for future use?
I would appreciate if you show me how to untangle this! :)
Thanx!

Re: Two IF's or query WHERE + AND problem

Posted: Sun Nov 27, 2011 2:20 am
by manohoo
Have you tried this?

Code: Select all

$query = "SELECT * FROM tableB WHERE b_id = $aid";
And yes, you have to close all "{"

Re: Two IF's or query WHERE + AND problem

Posted: Wed Nov 30, 2011 12:32 am
by unexperion
Of course it's not working... Why would I do that, mate? I get nothing by doing that ; I need second filter ; b_id should be different from $id ; but i need a_id to to be the one selected in the previous php - and for that specific a_id I need displayed other b_name, other than those that b_id had in the first php ; it's a question of writing proper line for 2 ifs ; but I just can't seem to find the way to do it right :( , and no one apparently wants to help me here :)