Primary Key gap filler

Copy Paste everything below the following line into a file and name it PrimaryKeyReset.php
From there follow my easy to understand directions from top to bottom. This works.
I spent a long time writing this script and I’d like it if you kept my name on it when you use it. However since I doubt any of you will do that, I’m not letting you take my variable names so I changed everything into numbers so it’s hard to understand. All you need to know is that it works and that you just need to fill out everything that says “Enter_Here” with your database information. They told me it couldn’t be done… So I did it.

Primary Key Reset and Organizer on New Database Table Row <?php $user = "Enter_User_Name_Here"; $password = "Enter_Password_Here"; $database = "Enter_Database_Name_Here"; $host = "Enter_Host_Name_Here_Or_localhost"; $table = "Enter_Table_Name_Here"; $primaryKeyID = "Enter_Primary_Key_Column_Name_Here";

$name=$_POST[‘txtContactName’];
$email=$_POST[‘txtContactEmail’];
$question=$_POST[‘txtContactQuestion’];
$insert = “INSERT INTO $table VALUES(’$primaryKey’,’$name’,’$email’,’$question’)”;

///////////////////////////////////////////////
//////////DO NOT EDIT BELOW THIS LINE//////////
///////////////////////////////////////////////
mysql_connect($host,$user, $password);
@mysql_select_db($database) or die( “Unable to select database”);
$55555555= 1;
$22222222 = “SELECT COUNT(’$primaryKeyID’) AS num FROM $table”;
$11111111 = mysql_query($22222222) or die(mysql_error());
$44444444 = mysql_fetch_assoc($11111111);
$33333333 = $44444444[‘num’];
if ( $33333333 > 1 )
{
$66666666= 1;
$77777777= $66666666;
while ($66666666<= $33333333)
{
$88888888= mysql_query(“SELECT * FROM $table WHERE $primaryKeyID = $77777777”);
$99999999= mysql_num_rows($88888888);
while ($99999999< 1)
{
$77777777= $77777777+ 1;
$88888888= mysql_query(“SELECT * FROM $table WHERE $primaryKeyID = $77777777”);
$99999999= mysql_num_rows($88888888);
if ($99999999!= 0)
{
mysql_query(“UPDATE $table SET $primaryKeyID=$66666666 WHERE $primaryKeyID = $77777777”);
}
}
$66666666= $66666666+ 1;
$77777777= $66666666;
}
}
$reset = “ALTER TABLE $table AUTO_INCREMENT = 1”;
mysql_query($reset);
mysql_query($insert) or die (“error updating database”);
mysql_close();
?>

Please feel free to reply about how this code works for you so others may see positive results and not assume that whole thing I wrote up there is an unsolved problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service