I have one problem with php and mysql. I have 3 tables (Users, User_status and Analysis) and want to display users and profit in a php table order by profit.
So I wrote this code:
$tipster = mysql_query("SELECT Analysis.Profit, Users.Username, User_status.id
FROM Analysis, Users
INNER JOIN User.status
ON Users.User_status_name=User_status.user_status
WHERE Users.User_status_name=User_status.user_status
AND User_status.id>=3
ORDER BY Profit
");
while ($row = mysql_fetch_array($tipster)) {
$sel_tipster=$row['Username'];
echo "<tr>";
echo "<td>";
echo $row['Username'];
echo "</td>";
$profit = mysql_query("SELECT SUM(Analysis.Profit), SUM(Analysis.Stake)
FROM Analysis
WHERE Analysis.Tipster='$sel_tipster'
AND Result>0
GROUP BY Tipster");
echo "<td>";
if (mysql_num_rows($profit)==0) {
echo 0; }
while ($row = mysql_fetch_array($profit)) {
$sel_profit = $row['SUM(Analysis.Profit)'];
echo $sel_profit;
echo "</td>";
echo "</tr>";
}
}
but I get multiple results. I have 4 users in mysql, but in php table I have 6x times these users. If I delete Analysis table from first select query than I've got 4 users.
I want to have only these users than are in db and order by profit. What is wrong with this code?
Table Users: id, Username, User_status_name
Table User_status: id, user_status
User_status_name form Users and user_status from User_status are in relation
Table Analysis: id_an, Profit, Result, Tipster