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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.