pureDesi Posted December 2, 2006 Share Posted December 2, 2006 I've got a small question, how do I find (not SET) the auto_increment value of a table (in mySQL).Thanks Link to comment https://forums.phpfreaks.com/topic/29180-finding-auto_increment-mysql/ Share on other sites More sharing options...
jsladek Posted December 2, 2006 Share Posted December 2, 2006 here is some code I use[code]$sql_feilds = "describe $current_table_name"; $result_feilds = connect($sql_feilds); while ($rows_feilds = mysql_fetch_array($result_feilds)) { fwrite($f, " print(\"<th>$rows_feilds[0]\");\n"); if ($rows_feilds[3] == 'PRI') {$pk='$row['.$rows_feilds[0].']';} } # end while ($rows_feilds = mysql_fetch_array($result_feilds))[/code]Might not make sense but it basically does a describe on the table and then checks the value of $rows_feilds[3] if it is a PK it will be PRI-John Link to comment https://forums.phpfreaks.com/topic/29180-finding-auto_increment-mysql/#findComment-133771 Share on other sites More sharing options...
taith Posted December 2, 2006 Share Posted December 2, 2006 if you have your autoincrement in the id column, this prolly the easiest and fastest...[code]$result=mysql_query("SELECT MAX(id) FROM users");$row=mysql_fetch_array($result);echo $row['MAX(id)'];[/code] Link to comment https://forums.phpfreaks.com/topic/29180-finding-auto_increment-mysql/#findComment-133785 Share on other sites More sharing options...
jcbarr Posted December 2, 2006 Share Posted December 2, 2006 The only problem with the above solution is if you insert IDs 51,52,53 and then you delete the entry for 53, that code is going to tell you that 53 is the next auto increment when in reality it is 54. Link to comment https://forums.phpfreaks.com/topic/29180-finding-auto_increment-mysql/#findComment-133829 Share on other sites More sharing options...
trq Posted December 2, 2006 Share Posted December 2, 2006 The question really needs to be asked. Why do you need this value? This smells of poor design. Link to comment https://forums.phpfreaks.com/topic/29180-finding-auto_increment-mysql/#findComment-133836 Share on other sites More sharing options...
pureDesi Posted December 2, 2006 Author Share Posted December 2, 2006 Thanks for the speedy replies :DAnd I'd rather not go in-depth on my coding styles :S Link to comment https://forums.phpfreaks.com/topic/29180-finding-auto_increment-mysql/#findComment-133904 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.