Click to See Complete Forum and Search --> : Winner of Charts PHP, MySQL


DonMArtin
12-01-2003, 07:36 AM
Dear Programmers,

I want to echo some fields of a MySQL-database's row, where on field has the MIN value. I successfully echo a chart limited to 10 ordered by this field with mysql_fetch_array() but I want the first one showing up again, as the winner. I coded:

$least="SELECT MIN(time) FROM table LIMIT 1";
$winner=mysql_query($least);

echo "<tr><td>".$winner."</td></tr>

I only see the resource ID of the winner.

Where is my misunderstanding?

Thanks

Alex

pyro
12-01-2003, 07:43 AM
How exactly are you trying to query the DB? MIN returns the smallest value from the column...

DonMArtin
12-01-2003, 08:06 AM
That's what I want. The database holds fields like name, city, email, time(of adding to the database). I want the fields:
name, city of the first, the winner. I already echo the Top 10 ordered by time. This chart-list works fine.

$sql="SELECT * FROM table ORDER BY time";
$chart=mysql_query($sql);
$number=mysql_num_rows($chart);

Now I loop through $number until I have the Top 10 and echo them with:
$topten=mysql_fetch_array($chart)

And now I want the first of them echoed somewhere else
again.With the code I should at least get the time of the winner, ok. But I don't. How can I look for a row where one field is the smallest value within it's column? Isn't that simple?

$least="SELECT MIN(time) FROM table LIMIT 1";
$winner=mysql_query($least);

Alex

pyro
12-01-2003, 08:17 AM
You probably want something more like this:

$least="SELECT * FROM `table` ORDER BY `time` ASC LIMIT 1";
$winner=mysql_query($least);

DonMArtin
12-01-2003, 08:22 AM
probably, but again I only get the resource ID of the winner.

Alex

pyro
12-01-2003, 08:24 AM
How are you printing out the results?

DonMArtin
12-01-2003, 08:28 AM
echo "<tr><td><u>The winner is:</u>".$winner."</td></tr>\n";

Alex

pyro
12-01-2003, 08:31 AM
That would be why... ;)

$winner will simply equal 1 or 0, depending on if the query was sucessful or not. You need to fetch the results:

$least = "SELECT * FROM `table` ORDER BY `time` ASC LIMIT 1";
$winner = mysql_query($least);
$data = mysql_fetch_array($winner);
# $data is an array of the values for each column

DonMArtin
12-01-2003, 08:44 AM
THANK YOU

now echoeing $data[name] brings up what I want.

Alex

pyro
12-01-2003, 08:45 AM
You are welcome. :)