0

I have two tables from which I need to display some data in the following manner:

From Table1, I want to select all the users which have the same id_ref. After I selected them, I want to display their number, and then each of them in a list, and also to show how many rows each of them has in Table2, but not all their rows, just certain ones that meet my condition. Also I would like to display the ID of the rows selected from Table2. My code for this is:

    $query = 'SELECT * FROM Table1  WHERE id_ref = '.$id_user.' AND active = 1 AND approved = 1 ORDER BY `id_aff` DESC';
$users_brought = mysql_query($query, $conn) or die(mysql_error());
$num_users_brought  = mysql_num_rows($users_brought);  


    while($user_bro = mysql_fetch_assoc($users_brought)):


    $query = 'SELECT * FROM Table2 WHERE id_aff = '.$user_bro['id_aff'].' AND ref = '.$income['id_user'].
$user_bro_brought = mysql_query($query, $conn) or die(mysql_error());
$num_user_bro_brought = mysql_num_rows($user_bro_brought); 

But something isn't working... Any ideas on this?

4

2 回答 2

0

You have a syntax error in this line

$query = 'SELECT * FROM Table2 WHERE id_aff = '.$user_bro['id_aff'].' AND ref = '.$income['id_user'].

You should format it as

$query = 'SELECT * FROM Table2 WHERE id_aff = '.$user_bro['id_aff'].' AND ref = '.$income['id_user'];

By the way, you should not use mysql_* functions as they are deprecated

于 2013-05-28T06:34:45.997 回答
0

You can join the two tables like

$query = 'SELECT * FROM Table1
         JOIN table Table2 
         ON Table2.id_aff = Table1.id_aff AND Table2.ref = Table1.id_user  
         WHERE id_ref = '.$id_user.' AND active = 1 AND approved = 1 
         ORDER BY `id_aff` DESC';

and more over you had a syntax error for second select query ,try to replace it with

$query = 'SELECT * FROM Table2 
          WHERE id_aff = '.$user_bro['id_aff'].' 
          AND ref = '.$income['id_user'];
于 2013-05-28T06:35:00.070 回答