How to view 2 tables within a mysql database?

Asked

Viewed 44 times

1

I’m new with sql and php.

is the following, I have an affiliate system that I need to bring a user’s affiliate list.

for that 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 magry_users table I have the id (which is the same id of referral_user_id) and the user name.

I want to bring the name here:

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

Thank you!

  • You can use INNER JOIN, example: http://blog.thiagobelem.net/relations-tabelas-no-mysql

1 answer

0


Starting from the premise that within the table magry_users 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.