note 31715 added to ref.mysql

From: Date: Thu, 01 May 2003 09:13:11 +0000
Subject: note 31715 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-47340@lists.php.net to get a copy of this message
To find the holes in a table like this: id| myField 3 | 6 4 | 0 5 | 1 6 | 5 7 | 4 I came up with this function: function findhole( $table, $field ) { $retval = 0; $qrytxt = "SELECT $field FROM $table ORDER BY $field ASC"; $query = mysql_query( $qrytxt ); $current = mysql_fetch_array( $query, MYSQL_ASSOC ); if( $current[$field] != 0 || mysql_num_rows( $query ) < 1 ) { // no initial values: return 0 return $retval; } else { do { $next = mysql_fetch_array( $query, MYSQL_ASSOC ); // if successor doesn't appear in the table if( $next[$field] != $current[$field] + 1) { $retval = $current[$field] + 1; break; } else { $current = $next; } } while( true ); } return $retval; } And... to _fill_ the holes: $fh = findhole( "myTable", "myField" ); // will return 2 mysql_query( "insert into myTable values( null, '$fh' )" ); I hope this is useful! Regards, André Hoogeland -- http://www.php.net/manual/en/ref.mysql.php http://master.php.net/manage/user-notes.php?action=edit+31715 http://master.php.net/manage/user-notes.php?action=delete+31715 http://master.php.net/manage/user-notes.php?action=reject+31715

« previous php.notes (#47340) next »