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 Quote Link to comment 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 Quote Link to comment 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] Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 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.