Jump to content

echo'ing highest numerical column value +1


knox203

Recommended Posts

Hello all! I need to echo out the highest number in a numerical MySQL column, plus one. Is this possible? I figured I should try "MAX(card_num)+1 as maxcardnum" inside my query and then echo out "maxcardnum" but this does not seem to work. I'm running PHP 5.1.4 and am not receiving any errors, it's just not echo'ing anything out. Am I missing something? Thanks!

 

<?
$kj = " select 
id,
emp_num,
card_num,
emp_fname,
emp_type,
withdrawal,
active,
emp_lname,
max(card_num)+1 as maxcardnum
from 
$db.$tbl group by id ". $kj_order_line. "";
$kj_result = mysql_query($kj) or die (mysql_error());
$kj_count = mysql_num_rows($kj_result);
$fields = mysql_fetch_assoc($kj_result);

	$row_counter = 1;

do
	{ $row_counter++;
$activate = $fields['active'];
$card_num = $fields['card_num'];
$id = $fields['id'];
?>

Link to comment
Share on other sites

Thanks for your reply, brio0987. Here's my revised code, now that I move my <? echo ?> outside of the text box, I can see it's spitting out all the values of the table in random order, not just one single value. Any ideas?

 

<?
$kj = " select 
id,
emp_num,
card_num,
emp_fname,
emp_type,
withdrawal,
active,
emp_lname,
max(card_num) as maxcardnum
from 
$db.$tbl group by id ". $kj_order_line. "";
$kj_result = mysql_query($kj) or die (mysql_error());
$kj_count = mysql_num_rows($kj_result);
$fields = mysql_fetch_assoc($kj_result);

	$row_counter = 1;

do
	{ $row_counter++;
$activate = $fields['active'];
$card_num = $fields['card_num'];
$id = $fields['id'];
$maxcardnum = $fields['maxcardnum']+1;
?>

<?
echo $maxcardnum;
?>

Link to comment
Share on other sites

Alright, I have a little more information. If I put the echo code AFTER "<? } while ($fields = mysql_fetch_assoc($kj_result)); ?>" it will work just fine... spits out "182" (181 is the highest number in the DB). But if I try to echo that BEFORE the while, it will spit out every value in the card_num column. Is there any way around this??

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.