Se abbiamo bisogno di recuperare il valore successivo di incremento automatico di una tabella (senza incrementare il valore stesso ovviamente), questa soluzione di seguito riportata potrà aiutarti.
1 2 3 4 5 6 7 8 9 |
<?php $query = mysql_query("SHOW TABLE STATUS WHERE name='your_table_name_here'"); if (mysql_num_rows($query)) { $result = mysql_fetch_assoc($query); echo $result['Auto_increment']; } else {//error //error control here } ?> |
Sostituisci soltanto “your_table_name_here�? col nome della tua tabella mySQL.
Inoltre, rendendo il tutto una funzione PHP la soluzione appare ancora più usabile.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php function get_next_value($tablename) { $query = mysql_query("SHOW TABLE STATUS WHERE name='".mysql_real_escape_string($tablename)."'"); if (mysql_num_rows($query)) { $result = mysql_fetch_assoc($query); return $result['Auto_increment']; } else {//error return false; } } //usage $nextval = get_next_value('your_table_name_here'); ?> |
pixolo ads