Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Remove null values from database

$
0
0

Hi.  I was curious if anyone had any other methods of removing those NULL rows in a database table.  If you don't use auto increment primary key for your database table then you don't have this problem.  But if you do use auto increment primary key then when a row is deleted for whatever reason, sql will delete the row values of every column except the the primary key column.  The primary key will remain and take up room in your database.  I have heard that other developers export the database and create a new table and then import the database back in.  That's how they solve this problem.  I prefer using php code.  But my question...
Question:  Is there another way to get rid of those NULL rows when using auto incrementing primary key in database table?

PS:  There is nothing wrong with the code below.  It works.

<?php
// DATABASE PRIMARY KEY GAP FILLER
// Developer: Ronnie Costa
// Purpose: Cleans up the database table from all the NULL rows.
// Explained: A database table with auto incrementing numbers as the primary key never deletes any primary keys.  Not even if you delete a row from the table. You will just have row 5 for example and no other data will be present because it was deleted for whatever reason. This rearranges the rows to fill those empty gaps.
// Directions: Modify the variable names to match the criteria of your database in includes dbConnect.php and dbTableName.php
// Name this file dbPKGapFiller.php
// Call this file from other php files like this <?php include 'dbPKGapFiller.php';?>
include 'dbConnect.php'; // Must be deleted if already called by another php file that initiated dbConnect.php.
include 'dbTableName.php';  // Must be deleted if already called by another php file that initiated dbTableName.
// _____________________________
// DO NOT EDIT BELOW THIS LINE
// _____________________________ 
$countFunction = "SELECT COUNT('$dbColumn1') AS num FROM $dbTableName"; // figures out how many rows there are in the database table.
$data = mysql_query($countFunction) or die(mysql_error());
$row = mysql_fetch_assoc($data);
$numRows = $row['num'];
if ( $numRows > 1 ) // If there is more than one row in the database table,
    {
    $currentRow = 1; // Counter that is used to keep track of what row it left off on after it finishes updating a row.
    $PKIDvalue = $currentRow; // Counter that is used to update the current row in the database table.
    while ($currentRow <= $numRows) // While the counter hasn't reached the number of rows in the table,
        {
        $rowIDquery = mysql_query("SELECT * FROM $dbTableName WHERE $dbColumn1 = $PKIDvalue"); // Select the primary key($dbColumn1) that matches the number on the counter($PKIDvalue).
        $rowID = mysql_num_rows($rowIDquery);
        while ($rowID < 1) // While there is a NULL row in the database table,
            {
            $PKIDvalue = $PKIDvalue + 1;
            $rowIDquery = mysql_query("SELECT * FROM $dbTableName WHERE $dbColumn1 = $PKIDvalue");
            $rowID = mysql_num_rows($rowIDquery);
            if ($rowID != 0) // If there is no primary key in the database table that matches the current counter number($PKIDValue),
                {
                mysql_query("UPDATE $dbTableName SET $dbColumn1=$currentRow WHERE $dbColumn1 = $PKIDvalue");
                }
            }
        $currentRow = $currentRow + 1; // Increases the counter($currentRow) so that it can check the next row of the database table.
        $PKIDvalue = $currentRow; // Resets the counter($PKIDvalue) that is used to update the row in the database table.
        }
    }
$reset = "ALTER TABLE $dbTableName AUTO_INCREMENT = 1";
mysql_query($reset); // Resets the primary key auto incrementor so that when it inputs a new row, it starts off in the new last row rather than the old one before it rearranged the rows.
?>

Viewing all articles
Browse latest Browse all 13200

Trending Articles