knox203 Posted November 13, 2007 Share Posted November 13, 2007 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']; ?> Quote Link to comment Share on other sites More sharing options...
bri0987 Posted November 13, 2007 Share Posted November 13, 2007 $result=msyql_query("SELECT MAX(id) FROM invoices"); $row=mysql_fetch_array(); $newid=$row['MAX(id)']+1; Quote Link to comment Share on other sites More sharing options...
knox203 Posted November 13, 2007 Author Share Posted November 13, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
knox203 Posted November 13, 2007 Author Share Posted November 13, 2007 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?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.