PDA

View Full Version : mysql max()



mjz
March 10th, 2004, 18:13
why does this return an error?



$query = "SELECT MAX(ordernr) FROM order";
$result = mysql_query($query)
or die("Error 1");
$row = mysql_fetch_array($result);


i thought that was the correct syntax, but maybe im wrong... oh and it returns the die error "Error 1", and theres nothing wrong with the mysql connection..

Pablo
March 10th, 2004, 18:49
when does that appear?
what r u using? phpbb, phpnuke, vbulletin, wot?

mjz
March 10th, 2004, 18:58
em... no, i wrote that little piece of code... it returns an error for some reason...

Pablo
March 10th, 2004, 19:03
im not sure about wot u r trying to do, but try:


or die(mysql_error());

bloodyveins
March 10th, 2004, 21:47
troubleshooting:
1.check whether ordernr is integer.
2. try this query
$query = "SELECT MAX(ordernr) AS max FROM order";
$result = mysql_query($query)
or die("Invalid query");
$row = mysql_fetch_array($result);
print $row[max];

wish this help
:coolmusic

Canuckkev
March 10th, 2004, 22:09
Well, you are trying to fetch an array from an integer, I think. That doesn't explain the Error from the SELECT query, but still...

When you "SELECT MAX(ordernr) FROM order" it should return the integer value of the maximum order number, and therefore you cannot fetch an array from that.

So, try "SELECT column1,column2,column3 FROM order ORDER BY ordernr DESC LIMIT 1". Then, fetch the array to get $array['column1'], $array['column2'], $array['column3'].

mjz
March 11th, 2004, 02:30
thanks for trying to help guys...

i tried "SELECT MAX(ordernr) AS max FROM order" but it still claims theres an error in my sql syntax...

i'll try that order by statement, thanks

mjz
March 11th, 2004, 02:54
argh! *slaps forehead*

its probably because i named the table "order"... let me see if thats it...

kabatak
March 11th, 2004, 10:33
$query = "SELECT MAX(ordernr) FROM order";
$result = mysql_query($query) or die(mysql_error());
$max = mysql_result($result, 0);

Canuckkev
March 11th, 2004, 19:42
Originally posted by mjz
argh! *slaps forehead*

its probably because i named the table "order"... let me see if thats it...

Could be...you will have to enclose the table with the funky quotes `table`. I actually don't know if that will fix it, but it should.