Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- cleanDB();
- function cleanDB() {
- $sv_name = "";
- $username = "";
- $password = "";
- $db_name = "";
- $con = mysql_connect($sv_name, $username, $password) or die(mysql_error());
- mysql_select_db($db_name, $con) or die(mysql_error());
- $table = "uni_subjects";
- // 1. delete all entries that are DELETED
- $query = "SELECT * FROM " . $table;
- $result = mysql_query($query, $con) or die(mysql_error());
- while($row = mysql_fetch_array($result))
- {
- if ($row['name'] == "DELETED") {
- $querydelete = "DELETE FROM " . $table . " WHERE id = " . $row['id'];
- mysql_query($querydelete, $con) or die(mysql_error());
- }
- }
- // 2. check number of remaining subjects ($numofsubs)
- $query = "SELECT * FROM " . $table;
- $result = mysql_query($query, $con) or die(mysql_error());
- $x = 1;
- while($row = mysql_fetch_array($result))
- {
- $x++;
- }
- $numofsubs = $x;
- // 3. order remaining subject entries properly
- // 3.1. go through all and set ids to $i + 1000
- // 3.2. go through all again, and set ids to $i
- for ($i=1; $i<$numofsubs; $i++) {
- $query = "SELECT * FROM " . $table;
- $result = mysql_query($query, $con) or die(mysql_error());
- $x = 1;
- while($row = mysql_fetch_array($result))
- {
- if ($x == $i && $i != $row['id']) {
- // set rowID to $i
- $tempi = $i+1000;
- $queryupdate = "UPDATE " . $table . " SET id = " . $tempi. " WHERE id = " . $row['id'];
- mysql_query($queryupdate, $con) or die(mysql_error());
- }
- $x++;
- }
- }
- for ($i=1; $i<$numofsubs; $i++) {
- $query = "SELECT * FROM " . $table;
- $result = mysql_query($query, $con) or die(mysql_error());
- $x = 1;
- while($row = mysql_fetch_array($result))
- {
- if ($x == $i && $i != $row['id']) {
- // set rowID to $i
- $queryupdate = "UPDATE " . $table . " SET id = " . $i . " WHERE id = " . $row['id'];
- mysql_query($queryupdate, $con) or die(mysql_error());
- }
- $x++;
- }
- }
- //echo "Database Cleaned. <br/> <a href=\"index.php\">Admin Page</a>";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement