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. 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 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. 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 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. 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
Archived
This topic is now archived and is closed to further replies.