Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. cleanDB();
  4.  
  5. function cleanDB() {
  6.  
  7. $sv_name = "";
  8. $username = "";
  9. $password = "";
  10. $db_name = "";
  11.  
  12. $con = mysql_connect($sv_name, $username, $password) or die(mysql_error());
  13. mysql_select_db($db_name, $con) or die(mysql_error());
  14. $table = "uni_subjects";
  15.  
  16.  
  17. // 1. delete all entries that are DELETED
  18. $query = "SELECT * FROM " . $table;
  19. $result = mysql_query($query, $con) or die(mysql_error());
  20. while($row = mysql_fetch_array($result))
  21. {
  22. if ($row['name'] == "DELETED") {
  23. $querydelete = "DELETE FROM " . $table . " WHERE id = " . $row['id'];
  24. mysql_query($querydelete, $con) or die(mysql_error());
  25. }
  26. }
  27.  
  28.  
  29.  
  30. // 2. check number of remaining subjects ($numofsubs)
  31. $query = "SELECT * FROM " . $table;
  32. $result = mysql_query($query, $con) or die(mysql_error());
  33. $x = 1;
  34. while($row = mysql_fetch_array($result))
  35. {
  36. $x++;
  37. }
  38. $numofsubs = $x;
  39.  
  40.  
  41.  
  42. // 3. order remaining subject entries properly
  43. // 3.1. go through all and set ids to $i + 1000
  44. // 3.2. go through all again, and set ids to $i
  45. for ($i=1; $i<$numofsubs; $i++) {
  46.  
  47. $query = "SELECT * FROM " . $table;
  48. $result = mysql_query($query, $con) or die(mysql_error());
  49. $x = 1;
  50. while($row = mysql_fetch_array($result))
  51. {
  52. if ($x == $i && $i != $row['id']) {
  53. // set rowID to $i
  54. $tempi = $i+1000;
  55. $queryupdate = "UPDATE " . $table . " SET id = " . $tempi. " WHERE id = " . $row['id'];
  56. mysql_query($queryupdate, $con) or die(mysql_error());
  57. }
  58. $x++;
  59. }
  60. }
  61. for ($i=1; $i<$numofsubs; $i++) {
  62.  
  63. $query = "SELECT * FROM " . $table;
  64. $result = mysql_query($query, $con) or die(mysql_error());
  65. $x = 1;
  66. while($row = mysql_fetch_array($result))
  67. {
  68. if ($x == $i && $i != $row['id']) {
  69. // set rowID to $i
  70. $queryupdate = "UPDATE " . $table . " SET id = " . $i . " WHERE id = " . $row['id'];
  71. mysql_query($queryupdate, $con) or die(mysql_error());
  72. }
  73. $x++;
  74. }
  75. }
  76. //echo "Database Cleaned. <br/> <a href=\"index.php\">Admin Page</a>";
  77.  
  78. }
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement