How to query 2 tables inside a mysql database? [duplicate]

1

I'm new to sql and php.

It's as follows, I have an affiliate system that I need to bring a user's affiliate list to.

For this I use:

<?php
//traz os usuarios
$query = sprintf("SELECT referral_user_id FROM magry_awoaffiliate_referral where affiliate_user_id = '$userid'");
$dados = mysql_query($query, $con) or die(mysql_error());
$linha = mysql_fetch_assoc($dados);
$total = mysql_num_rows($dados);
$teste = $linha['referral_user_id'];
?>

<?php
    if($total > 0) {
        do {
?>
<ul>
            <li><?=$linha['referral_user_id']?></li>
</ul>
<?php
        }while($linha = mysql_fetch_assoc($dados));
    }

mysql_free_result($dados);
?>

This query brings me the list of IDs of this user, but I need to get the name of each user that is in another table (magry_users).

How do I bring this information?

In the affiliate table, I have the referral_user_id that does not have its name and in the table magry_users I have the id (which is the same id as referral_user_id) and the username.

I want to bring the name here:

<li><?=$linha['referral_user_id']?> - "nome do usuario"</li>

Thank you!

    
asked by anonymous 16.01.2017 / 22:19

1 answer

0

From the premise that within the magry_users table the fields use their names as id e nome , use this mySQL call:

$query = sprintf("SELECT 'magry_awoaffiliate_referral'.'referral_user_id', 'magry_users'.'nome' FROM 'magry_awoaffiliate_referral'
  INNER JOIN 'magry_users' ON 'magry_awoaffiliate_referral'.'referral_user_id' = 'magry_users'.'id'
  WHERE 'magry_awoaffiliate_referral'.'referral_user_id'='".$userid."' ORDER BY 'magry_awoaffiliate_referral'.'referral_user_id' DESC");

After that you can use:

<li><?=$linha['referral_user_id'] . ' - ' . $linha['nome']?></li>
    
16.01.2017 / 22:38