login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Database tools > Mysql Class

Mysql Class

Auteur: delta_004 - 20 februari 2007 - 14:14 - Gekeurd door: Rens - Hits: 5668 - Aantal punten: (0 stemmen)




Script Volgt

Ik was me localhost aan het leeghalen, toen kwam ik dit script tegen. Het is een goed werkend script maar er zullen nog zeker een aantal dingen verbeterd kunnen worden. Maja het werkt;)


Code:
  1. <?php
  2. function microtime_float () {
  3. list ($msec, $sec) = explode(' ', microtime());
  4. return (float)$msec + (float)$sec;
  5. }
  6.  
  7. class SQL {
  8. //**Vars**\\
  9. var $tijd;
  10. var $connectie;
  11. var $querys = 0;
  12. var $fetchs = 0;
  13. var $assocs = 0;
  14. var $arrays = 0;
  15. var $objects = 0;
  16. var $numrows = 0;
  17.  
  18. //**Function Connect**\\
  19. function connect($host="localhost", $username="", $password="", $database="") {
  20. $microtime_start = microtime_float(); // Start Time
  21. if(empty($host)) { // Empty Hostname
  22. die($this->_error("Please fill in a host!"));
  23. } elseif(empty($database)) { // Empty Database
  24. die($this->_error("Please fill in a database!"));
  25. } else {
  26. if(mysql_connect($host, $username, $password)) { // Connection DB
  27. $this->connectie = "Geconnecteerd met ".$host; //
  28. } else { // False
  29. $this->connectie = "Connectie mislukt met ".$host;
  30. die($this->_error(mysql_error()));
  31. }
  32. if(mysql_select_db($database)) { // Select DB
  33. $this->connectie = "Geconnecteerd met ".$host;
  34. } else { // False
  35. $this->connectie = "Connectie mislukt met ".$host;
  36. die($this->_error(mysql_error()));
  37. }
  38. }
  39. $microtime_end = microtime_float(); // End Time
  40. $microtime = $microtime_end - $microtime_start; // Time
  41. $this->tijd += $microtime; // Total Time
  42. }
  43. //**Function Mysql_query**\\
  44. function query($query) {
  45. $microtime_start = microtime_float(); // Start Time
  46. $this->querys ++; // Counter
  47. return mysql_query($query); // Return Query
  48. $microtime_end = microtime_float(); // End Time
  49. $microtime = $microtime_end - $microtime_start; // Time
  50. $this->tijd += $microtime; // Totel Time
  51. }
  52. //**Function Mysql_num_rows**\\
  53. function num_rows($query) {
  54. $microtime_start = microtime_float(); // Start Time
  55. $this->numrows ++; // Counter
  56. return mysql_num_rows($query); // Return Num Rows
  57. $microtime_end = microtime_float(); // End Time
  58. $microtime = $microtime_end - $microtime_start; // Time
  59. $this->tijd += $microtime; // Total Time
  60. }
  61. //**Function Fetch_Assoc**\\
  62. function fetch_assoc($query) {
  63. $microtime_start = microtime_float(); // Start Time
  64. return mysql_fetch_assoc($query); // Return Fatch_assoc
  65. $microtime_end = microtime_float(); // End Time
  66. $microtime = $microtime_end - $microtime_start; // Total Time
  67. $this->tijd += $microtime; // Total Time
  68. $this->assocs ++; // Counter
  69. }
  70. //**Function Fetch_Object**\\
  71. function fetch_object($query) {
  72. $microtime_start = microtime_float(); // Start Time
  73. return mysql_fetch_object($query); // Return Fatch_assoc
  74. $microtime_end = microtime_float(); // End Time
  75. $microtime = $microtime_end - $microtime_start; // Total Time
  76. $this->tijd += $microtime; // Total Time
  77. $this->objects ++; // Counter
  78. }
  79. //**Function Fetch_Array**\\
  80. function fetch_array($query) {
  81. $microtime_start = microtime_float(); // Start Time
  82. return mysql_fetch_array($query); // Return Fatch_assoc
  83. $microtime_end = microtime_float(); // End Time
  84. $microtime = $microtime_end - $microtime_start; // Total Time
  85. $this->tijd += $microtime; // Total Time
  86. $this->arrays ++; // Counter
  87. }
  88. //**Function Set Increment**\\
  89. function setIncrement($veld, $tabel) {
  90. $sql = mysql_query("SELECT ".$veld." FROM ".$tabel) or die(mysql_error());
  91. while ($object = mysql_fetch_object($sql)) {
  92. $array[] = $object->{$veld};
  93. }
  94. for ($i = 0; $i < count($array); $i++) {
  95. mysql_query("UPDATE ".$tabel." SET ".$veld." = ".($i + 1)." WHERE ".$veld." = ".$array[$i]);
  96. }
  97. }
  98. //**Function Error**\\
  99. function _error ($error="") {
  100. echo "Error: ".$error; // Echo: Var Error
  101. }
  102. // Acties: Logboek/Statistieken
  103. function getInfo() {
  104. $return = "<h1>Statistics:</h1>";
  105. $return .= "Connectie: ".$this->connectie."<br>";
  106. $return .= "Count Querys: ".$this->querys."<br>";
  107. $return .= "Count Fetch: ".($this->assocs + $this->arrays + $this->objects)."<br>";
  108. $return .= "Count Fetch Assocs: ".$this->assocs."<br>";
  109. $return .= "Count Fetch Arrays: ".$this->arrays."<br>";
  110. $return .= "Count Fetch Objects: ".$this->objects."<br>";
  111. $return .= "Count Num Rows: ".$this->numrows."<br>";
  112. $return .= "Time: ".round($this->tijd,6);
  113. return $return;
  114. }
  115. }
  116. //*Gebruik*\\
  117. $sql = new SQL();
  118. $sql->connect("localhost", "user", "pass", "db");
  119. $query = $sql->query("SELECT * BLA WHERE BLA='BLA'");
  120. while($assoc = $sql->fetch_assoc($query)) {
  121. echo $assoc['WOOT'];
  122. }
  123. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (8)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.042s