greencoin Posted June 19, 2007 Share Posted June 19, 2007 I'm trying to query the database, return the 5 digit ID + 1 and echo it into a text field. I have it all working but I'm losing my zeros! MySQL shows 00001 but my page displays 1. The ID is stored in MySQL as mediumint(5), unassigned zerofill. The php query is as follows; $query2 = mysql_query("SELECT MAX(iid+1) AS maximum FROM gc_prod_info") or die(mysql_error()); Do I need to change my query to SELECT MAX(iid+00001) ...? Any advice is appreciated. Thanks ~Rich Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 19, 2007 Share Posted June 19, 2007 my sql consider 00001 as 1 so get 1 as an output all the zero at the left ignored. now to have the complete number of character use 10000 Quote Link to comment Share on other sites More sharing options...
greencoin Posted June 19, 2007 Author Share Posted June 19, 2007 so are you saying that my numeric sequence has to start @ 10001 instead of 00001? ~Rich Quote Link to comment Share on other sites More sharing options...
Illusion Posted June 19, 2007 Share Posted June 19, 2007 try this SELECT CAST(MAX(iid+1) AS VARCHAR) AS maximum FROM gc_prod_info or SELECT CAST(MAX(iid) AS VARCHAR)+1 AS maximum FROM gc_prod_info but I am not sure.............. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 19, 2007 Share Posted June 19, 2007 Use either the function sprintf() to format the string or printf() to print a formated string with a zero filled field. <?php $i = 1; printf("%05d",$i); ?> Ken Quote Link to comment Share on other sites More sharing options...
greencoin Posted June 19, 2007 Author Share Posted June 19, 2007 try this SELECT CAST(MAX(iid+1) AS VARCHAR) AS maximum FROM gc_prod_info or SELECT CAST(MAX(iid) AS VARCHAR)+1 AS maximum FROM gc_prod_info but I am not sure.............. I'm not sure changing it to a VARCHAR will work as I'm pushing it back into MySQL as an INT when the form is submitted... ~Rich Quote Link to comment Share on other sites More sharing options...
Illusion Posted June 19, 2007 Share Posted June 19, 2007 I'm not sure changing it to a VARCHAR will work as I'm pushing it back into MySQL as an INT when the form is submitted... ~Rich You can submit it as an INT, no problem. 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.