Right . . . put another way . . . .
first result -> returns first row in table 1, both in table two. (2)
second result -> returns first row in table 1, both in table two. (4)
.. and so on. What you want here is a join. Before you go there though,
I'm trying to output the total number of MySQL rows
Is this really all you want in this query? If it is, select * is inefficient on two counts. First, if you don't need
all values in a recordset, you should always do
select field1, field2 from table
and second, if all you need is a count, selecting the actual rows is a huge overhead and unnecessary. It's also a bad habit to get into for counting results, for example, if you start to set pagination, mysql_num_rows will always be equal to the number of items per page. mySQL has a function for counting total rows: count(fieldname) or count(*).
HOWEVER - if you want a count AND will be outputting row data, your approach is probably fine. Get just a count:
$query = "select count(*) from members, teams where members.team_id=teams.team_id";
$results = mysql_query($query);
$row = mysql_fetch_array($results);
$totalParticipants = $results[0];
- mysql joins require the table prefix to avoid ambiguities (i.e., an id field in both tables, which are you referring to?)
- note mysql_fetch_array returns *both* the indexed and associative arrays.
But if you'll be outputting results,
$query = "select * from members, teams where members.team_id=teams.team_id";
$results = mysql_query($query);
$totalParticipants = $mysql_num_rows($results);
echo "<p>Total participants: $totalParticipants</p>";
while ($row = mysql_fetch_array($results)) {
echo "<p>" . $row[0] . " " . $row[1] . " " . $row[2] . "</p>";
}
if you select specific fields, you don't have to use the indexed array. Still have to prefix the fields.
$query = "select members.fname,members.lname,teams.teamname from members, teams where members.team_id=teams.team_id";
echo "<p>" . $row['fname'] . " " . $row['lname'] . " " . $row['teamname'] . "</p>";