php/sql select issue - help needed!

MMD

MMD

Associate
Joined
22 May 2003
Posts
382
My problem is I want to select the highest 'id' row from my db (mysql) and output/store the data in variables.

So far I can do it this way;

PHP:
	$result = mysql_query("SELECT MAX(id) FROM games");
	$row = mysql_fetch_array($result);
	
	echo $row[0];


but I want to use:
PHP:
echo $row['id'];
for ease of use, but it won't work - I don't get any output. Same for if I use mysql_fetch_assoc()


Any ideas?
 
I kind of came up with that anyway;

PHP:
$result = mysql_query("SELECT opposition, MAX(id) FROM games");
	$row = mysql_fetch_array($result);
	
	echo $row['id'];
	echo $row['opposition'];

but it doesn't work
 
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/finest1/public_html/predictionleague/seasontwo/admin/updategame.php on line 25

Is the error I get.
 
added an 'or die' and this is the new error;

Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
 
You query doesn't have a group by clause - try this:

PHP:
$result = mysql_query("SELECT opposition, MAX(id) FROM games GROUP BY opposition");
    $row = mysql_fetch_array($result);
    
    echo $row['id'];
    echo $row['opposition'];
 
If you just want the last ID, why not just LIMIT 1 and make it DESC or ASC depending which one it gets?

If not i got wrong end of this thread and im sorry :P
 
Could have used that, but it's not fool proof I don't think.

I have used two queries instead of one and the result is fine..

Another question though!;

If I have a 'league table' and I want to output it to seperate tables, how would I do it?
What I mean by this is, I can output it in order fine to a single table, but with over 100 users it looks ugly as the page scrolls forever.

Is there any way of writing the output to multiple tables (html)? ie; first 25 users in table1, next to that 26-50 users in table2 and so on?

Any ideas?
so far I have;
PHP:
<table width="200">
  <tr>
    <td>Username</div></td>
    <td>Score</div></td>
  </tr>
  
<?
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "<tr><td>" . $row['username'] . "</td>" . $row['score'] . "</td></tr>";
}
?>
</table>
which works fine.. but is just a single table, not multiple.
 
Back
Top Bottom