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
https://forums.phpfreaks.com/topic/77071-echoing-highest-numerical-column-value-1/
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;
?>

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

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.