archux Posted November 27, 2008 Report Posted November 27, 2008 Sveiki, vai ir kaut kādā veidā iespējams izvilkt no datubāzes next Autoindex vai arī pēdējo tipa last index no datubāzes? Ja jā kāds tas izvelkamais parametrs izskatītos un kā ar to darboties?
archux Posted November 27, 2008 Author Report Posted November 27, 2008 atradu pats, ir divi varianti: $q = mysql_query("show table status from DATABASE like 'TABLE'") or die(mysql_error()); print mysql_result($q, 0, 'Auto_increment'); // which prints the next autoincrement value un otrs: $sql = 'SELECT MAX(thread_id) AS max_thread_id FROM threads'; this creates a variable for an SQL query that gets the highest value in the "thread_id" column using the MAX() aggregate function and stores it in a varibale called "max_thread_id". $result = mysql_query ($sql); ...runs the query and stores the result set as $result $array = mysql_fetch_assoc ($result); ...spools the result set into an associative array extract ($array); ...extracts out the array element as a global variable so now you have a variable $max_thread_id that is the last thread_id that was inserted. All of the above code can be done in a single line: extract (mysql_fetch_assoc (mysql_query ("SELECT MAX(thread_id) AS max_thread_id FROM threads"))); then add 1 to that varibale: $next_autoindex = $max_thread_id + 1; $next_autoindex is the value of whatever the next insert auto_incerement will be. vairāk info te: http://www.phpbuilder.com/board/archive/in...t-10218213.html
marcis Posted November 27, 2008 Report Posted November 27, 2008 foršāks nebūs codez ieteiktais variants? mysql_query("INSERT INTO ..."); $id=mysql_insert_id();
Kaklz Posted November 27, 2008 Report Posted November 27, 2008 Izskatās, ka cilvēks grib zināt to auto-id pirms ieraksta ielikšanas tabulā, kas man liekas varētu nebūt pārāk labs risinājums.
Gints Plivna Posted November 27, 2008 Report Posted November 27, 2008 extract (mysql_fetch_assoc (mysql_query ("SELECT MAX(thread_id) AS max_thread_id FROM threads")));then add 1 to that varibale: $next_autoindex = $max_thread_id + 1; $next_autoindex is the value of whatever the next insert auto_incerement will be. Kaut kā tas viss izskatās traki aizdomīgi gadījumā, ja ar to sistēmu strādā vairāk nekā 1 lietotājs... Gints Plivna http://datubazes.wordpress.com
Recommended Posts