Jump to content

[SOLVED] using SELECT Max(field)


severndigital

Recommended Posts

i am trying to find the max number entered into a field and i am having some difficulty.

 

the query is quite basic, i thought here is my code

 

<?php

$getit = mysql_query("SELECT MAX(Mnum) FROM masters");

$workit = $getit + 1;

echo $workit;

?>

 

basically i want to find the MAX value and add 1 to it.

 

the max value in this case is 300001

 

when i run this code however, $workit returns a value of 8.

 

what am i doing wrong??

 

thanks in advance.

 

chris

 

 

Link to comment
https://forums.phpfreaks.com/topic/60385-solved-using-select-maxfield/
Share on other sites

mysql_query() does not return the actual result of your query.  what it returns is a resource that POINTS to your results.  you need to use an extraction function such as mysql_result() or mysql_fetch_assoc() to actually pull the results from that resource:

 

$getit = mysql_query("SELECT MAX(Mnum) FROM masters");
$current_max_val = mysql_result($getit, 0, 0);
$workit = $current_max_val + 1;
echo $workit;

 

check the manual for more info.

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.