note 31715 deleted from ref.mysql by betz

From: Date: Wed, 21 Apr 2004 10:16:30 +0000
Subject: note 31715 deleted from ref.mysql by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-68425@lists.php.net to get a copy of this message
Note Submitter: andre.hoogeland@zonnet.nl ---- 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

« previous php.notes (#68425) next »