Jump to content

Minimum and Maximum Values


aenigma

Recommended Posts

This is what I have so far:
[code]$maxvalue = "SELECT MAX(id) AS id FROM news";
$maxresult = mysql_query($maxvalue);
$minvalue = "SELECT MIN(id) AS id FROM news";
$minresult = mysql_query($minvalue);

echo "Max ID is: $maxresult - Mininum ID is: $minresult";[/code]

But, all I get is: Max ID is: Resource id #4 - Mininum ID is: Resource id #5
Link to comment
https://forums.phpfreaks.com/topic/8935-minimum-and-maximum-values/
Share on other sites

[code]
$maxvalue = "SELECT MAX(id) AS id FROM news";
$maxresult = mysql_query($maxvalue);
$minvalue = "SELECT MIN(id) AS id FROM news";
$minresult = mysql_query($minvalue);

$max = mysql_fetch_array($maxvalue);
$min = mysql_fetch_array($minvalue);

echo "Max ID is: " . $max['id'] .  " - Mininum ID is: " . $min['id'];
[/code]

totally quick and dirty and could be done way better. but it works.
Ahh, figured it out, you had a small error though. You gave me this:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$maxvalue = "SELECT MAX(id) AS id FROM news";
$maxresult = mysql_query($maxvalue);
$minvalue = "SELECT MIN(id) AS id FROM news";
$minresult = mysql_query($minvalue);

$max = mysql_fetch_array([!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$maxvalue[!--colorc--][/span][!--/colorc--]);
$min = mysql_fetch_array([!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$minvalue[!--colorc--][/span][!--/colorc--]);

echo "Max ID is: " . $max['id'] . " - Mininum ID is: " . $min['id'];[/quote]

and it needed to be:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$maxvalue = "SELECT MAX(id) AS id FROM news";
$maxresult = mysql_query($maxvalue);
$minvalue = "SELECT MIN(id) AS id FROM news";
$minresult = mysql_query($minvalue);

$max = mysql_fetch_array([!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$maxresult[!--colorc--][/span][!--/colorc--]);
$min = mysql_fetch_array([!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$minresult[!--colorc--][/span][!--/colorc--]);

echo "Max ID is: " . $max['id'] . " - Mininum ID is: " . $min['id'];[/quote]
well that means that you are not executing a valid query. put

SELECT MIN(id) AS id FROM news

directly into phpmyadmin see if it returns a valid result. If it doesn't, then there's your problem. invalid query. If it does, then you may not be connecting to the database at all.

edit: my bad. i used the wrong variables. should be

$max = mysql_fetch_array($maxresult);
$min = mysql_fetch_array($minresult);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.