Solarpitch Posted June 25, 2007 Share Posted June 25, 2007 Hi, I want to be able to get the highest value from my user_id field in the database and increment it by 1. So highest user id is 104... next user will be 105 etc. I have the below code but it doesn't seem to be doing. It just puts the value in as 1 all the time. the job. Anyone help a little? I got the following code from a helpful guy which works fine for him. So I am a little stumped! :-\ $query = mysqli_query("SELECT MAX(user_id) `user_id` FROM `phpbb_users`"); $fetch= mysqli_fetch_assoc($mysqli_connect, $query); $get_val= $fetch['user_id']; $get_val++; Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 25, 2007 Share Posted June 25, 2007 Try this: <?php $query = mysqli_query("SELECT MAX(user_id)+1 AS max FROM `phpbb_users`"); $fetch= mysqli_fetch_assoc($mysqli_connect, $query); echo $fetch['max']; ?> Quote Link to comment Share on other sites More sharing options...
Jenk Posted June 25, 2007 Share Posted June 25, 2007 Just don't insert a value. If the field is an auto-incremental field, the database will automatically assign the next increment for you. You can then fetch that number with mysql_insert_id(); Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted June 25, 2007 Author Share Posted June 25, 2007 Hi Thanks for that code . . but it now inserts '0' as the value. Jenk, the field in phpbb is not an auto increment field. And I am not messing with phpbb db structure! Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 25, 2007 Share Posted June 25, 2007 Can you echo before and after the increment so: $get_val= $fetch['user_id']; echo $get_val; $get_val++; echo $get_val; ~ Chocopi Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted June 25, 2007 Author Share Posted June 25, 2007 I am trying to change the structure of the query.. I think thats the problem something like..? $query = ("SELECT MAX(user_id) `user_id` FROM `phpbb_users`"); $result = mysqli_query($mysqli_connect, $query); $fetch = mysqli_fetch_assoc($result); $get_val = $fetch['user_id']; $get_val++; Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 25, 2007 Share Posted June 25, 2007 Hmmm, I tested the last code I gave you on my own website, and it worked fine. Try this: <?php $query = ("SELECT MAX(user_id) AS max FROM `phpbb_users`"); $result = mysqli_query($mysqli_connect, $query); $fetch = mysqli_fetch_assoc($result); $get_val = $fetch['max']; $get_val++; echo $get_val; ?> Tell me what that echoes out, or if it works right. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted June 25, 2007 Author Share Posted June 25, 2007 pocobueno1388 fantastic! That works perfect. Thanks evryone for your help with this. And chocopi for supplying the initial code! ... Now I can get a cup of tea 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.