Cooper94 Posted February 20, 2009 Share Posted February 20, 2009 $result=mysql_query("SELECT * FROM pilots ORDER BY date"); $num=mysql_rows_num($result); $row = mysql_fetch_assoc($result); $a = {$row['username']}; $a++; $a; How can I make $a equal the last username registered. So say the last one was RPA101 so the next would be RPA102. Quote Link to comment https://forums.phpfreaks.com/topic/146113-auto/ Share on other sites More sharing options...
Maq Posted February 20, 2009 Share Posted February 20, 2009 Try this, I added some error checking and altered your SQL: $sql="SELECT * FROM pilots ORDER BY date DESC LIMIT 1"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($result); $a = $row['username']; echo "Last User: " . $a; Quote Link to comment https://forums.phpfreaks.com/topic/146113-auto/#findComment-767055 Share on other sites More sharing options...
shadiadiph Posted February 20, 2009 Share Posted February 20, 2009 you could make it your primary key for example if your primary key is pilotid when you call it fro your database or want to use it preceed it with RPA<?=$pilotid?> Quote Link to comment https://forums.phpfreaks.com/topic/146113-auto/#findComment-767081 Share on other sites More sharing options...
Maq Posted February 20, 2009 Share Posted February 20, 2009 I think I may have only answered part of your question. You would have to strip off the integer value from RPA and one it. You can ++ a string but when it reaches the max digit it will add a letter to your RPA. Quote Link to comment https://forums.phpfreaks.com/topic/146113-auto/#findComment-767085 Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2009 Share Posted February 20, 2009 To prevent problems with concurrent registrations (on modern multi-tasking, interrupt/event/service driven operating systems, you cannot guarantee the order in which queries are executed), you must do one of the following to insure that you don't repeat a value - Use an auto increment field for the number (prepend your letter code when it is retrieved) and let the database manage the generation of the next number. Make the column a unique key and check for an error if you try to insert a duplicate value. Loop, incrementing and attempting to insert until you successfully insert a value. Lock the table before the SELECT query and unlock it after an INSERT query so that you know the value you are generating and using is unique. Quote Link to comment https://forums.phpfreaks.com/topic/146113-auto/#findComment-767103 Share on other sites More sharing options...
shadiadiph Posted February 20, 2009 Share Posted February 20, 2009 PFMaBiSmAd a bit like what i suggested? Quote Link to comment https://forums.phpfreaks.com/topic/146113-auto/#findComment-767111 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.