Mutley Posted March 10, 2007 Share Posted March 10, 2007 I have a form that uploads an image, that I want to rename the image to the next auto_increment number that is in a field (id) of a row. How do I do this? I was trying to SELECT the last "id" and add 1 but obviously if I deleted a row, then went to add another picture, it would be 1 behind/incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/42128-finding-out-current-auto_increment-number/ Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 The best bet is to use the mysql_insert_id(); function. See below for reading on that. http://us3.php.net/manual/en/function.mysql-insert-id.php --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42128-finding-out-current-auto_increment-number/#findComment-204332 Share on other sites More sharing options...
mattd8752 Posted March 10, 2007 Share Posted March 10, 2007 I believe you would select all records, and loop through them, making PHP set a variable to the number if that is higher than the currently open variable. What I mean is: $curr = SQL_QUERY; if($curr > $x){ $x = $curr; } I'm not sure if there is another method. Quote Link to comment https://forums.phpfreaks.com/topic/42128-finding-out-current-auto_increment-number/#findComment-204335 Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 <?php $sql = "SELECT id FROM tableName ORDER BY id ASC LIMIT 1"; $qu = mysql_query($qu); $id = mysql_fetch_array($qu); $id = $id[0]; ?> --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42128-finding-out-current-auto_increment-number/#findComment-204342 Share on other sites More sharing options...
Mutley Posted March 10, 2007 Author Share Posted March 10, 2007 The best bet is to use the mysql_insert_id(); function. See below for reading on that. http://us3.php.net/manual/en/function.mysql-insert-id.php --FrosT Woah, easiest PHP yet, thanks. Worked straight away. Quote Link to comment https://forums.phpfreaks.com/topic/42128-finding-out-current-auto_increment-number/#findComment-204473 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.