class.sql.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. class sqlConnection
  3. {
  4. var $sql_query = array();
  5. var $error = array(" ");
  6. var $conn = "";
  7. var $dbs_type = "MySQL";
  8. var $dbs_hostname = "localhost";
  9. var $dbs_username = "root";
  10. var $dbs_password = "";
  11. var $dbs_database = "";
  12. var $dbs_backup = "";
  13. var $dbs_error = "";
  14. var $error_log = true;
  15. var $error_table = "fehler_sql";
  16. function getmicrotime ()
  17. {
  18. list($usec, $sec) = explode(" ",microtime());
  19. return ((float)$usec + (float)$sec);
  20. }
  21. function sql_connect ()
  22. {
  23. switch ($this->dbs_type)
  24. {
  25. case "MySQL":
  26. if (!@$this->conn = mysql_connect($this->dbs_hostname, $this->dbs_username, $this->dbs_password))
  27. {
  28. die($this->dbs_error);
  29. }
  30. mysql_select_db($this->dbs_database, $this->conn) or die(mysql_error());
  31. break;
  32. case "MSSQL":
  33. if (!@$this->conn = mssql_connect($this->dbs_hostname, $this->dbs_username, $this->dbs_password))
  34. {
  35. die($this->dbs_error);
  36. }
  37. mssql_select_db($this->dbs_database, $this->conn) or die(mssql_get_last_message());
  38. break;
  39. }
  40. }
  41. function sql_exec ($sql)
  42. {
  43. switch ($this->dbs_type)
  44. {
  45. case "MySQL":
  46. $time_start = $this->getmicrotime();
  47. $erg = mysql_query($sql, $this->conn);
  48. $time_end = $this->getmicrotime();
  49. $time = $time_end - $time_start;
  50. $error = mysql_error($this->conn);
  51. $errno = mysql_errno($this->conn);
  52. if ("" == $errno) $errno = 0;
  53. if ($erg && 0 == $errno) $zeilen = @mysql_num_rows($erg)+0; else $zeilen = -1;
  54. array_push($this->sql_query,array($sql,$zeilen,mysql_affected_rows($this->conn),$errno,$error,$time));
  55. array_push($this->error,$error);
  56. break;
  57. case "MSSQL":
  58. $time_start = $this->getmicrotime();
  59. $erg = @mssql_query($sql,$this->conn);
  60. $time_end = $this->getmicrotime();
  61. $time = $time_end - $time_start;
  62. $error = mssql_get_last_message();
  63. if (strpos($error,"Datenbankkontext"))
  64. {
  65. $error = " ";
  66. }
  67. if ("" == $error || in_array($error,$this->error))
  68. {
  69. $zeilen = @mssql_num_rows($erg) + 0;
  70. $errno = 0;
  71. $error = "";
  72. }
  73. else
  74. {
  75. $zeilen = -1;
  76. $errno = 1;
  77. }
  78. array_push($this->sql_query,array($sql,$zeilen,mssql_rows_affected($this->conn),$errno,$error,$time));
  79. array_push($this->error,$error);
  80. break;
  81. }
  82. if ($this->error_log && $errno != 0)
  83. {
  84. $sql2 = "INSERT INTO {$this->error_table} VALUES ('{$errno}',\"{$error}\",\"{$sql}\")";
  85. switch ($this->dbs_type)
  86. {
  87. case "MySQL":
  88. $erg2 = mysql_query($sql2,$this->conn);
  89. break;
  90. case "MSSQL":
  91. $erg2 = mssql_query($sql2,$this->conn);
  92. break;
  93. }
  94. }
  95. return $erg;
  96. }
  97. function sql_fetch_array ($erg)
  98. {
  99. switch ($this->dbs_type)
  100. {
  101. case "MySQL":
  102. $value = mysql_fetch_array($erg);
  103. break;
  104. case "MSSQL":
  105. $value = mssql_fetch_array($erg);
  106. break;
  107. }
  108. return $value;
  109. }
  110. function sql_fetch_assoc ($erg)
  111. {
  112. if ($erg)
  113. {
  114. switch ($this->dbs_type)
  115. {
  116. case "MySQL":
  117. $value = mysql_fetch_assoc($erg);
  118. break;
  119. case "MSSQL":
  120. $value = mssql_fetch_assoc($erg);
  121. break;
  122. }
  123. return $value;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. function sql_fetch_row ($erg)
  131. {
  132. if ($erg)
  133. {
  134. switch ($this->dbs_type)
  135. {
  136. case "MySQL":
  137. $value = mysql_fetch_row($erg);
  138. break;
  139. case "MSSQL":
  140. $value = mssql_fetch_row($erg);
  141. break;
  142. }
  143. return $value;
  144. }
  145. else
  146. {
  147. return false;
  148. }
  149. }
  150. function sql_result ($erg,$row,$col)
  151. {
  152. if ($erg)
  153. {
  154. switch ($this->dbs_type)
  155. {
  156. case "MySQL":
  157. $value = mysql_result($erg,$row,$col);
  158. break;
  159. case "MSSQL":
  160. $value = mssql_result($erg,$row,$col);
  161. break;
  162. }
  163. return $value;
  164. }
  165. else
  166. {
  167. return false;
  168. }
  169. }
  170. function sql_num_rows ($erg)
  171. {
  172. if ($erg)
  173. {
  174. switch ($this->dbs_type)
  175. {
  176. case "MySQL":
  177. $value = mysql_num_rows($erg);
  178. break;
  179. case "MSSQL":
  180. $value = mssql_num_rows($erg);
  181. break;
  182. }
  183. return $value;
  184. }
  185. else
  186. {
  187. return 0;
  188. }
  189. }
  190. function sql_affected_rows ()
  191. {
  192. if ($erg)
  193. {
  194. switch ($this->dbs_type)
  195. {
  196. case "MySQL":
  197. $value = mysql_affected_rows($this->conn);
  198. break;
  199. case "MSSQL":
  200. $value = mssql_rows_affected($this->conn);
  201. break;
  202. }
  203. return $value;
  204. }
  205. else
  206. {
  207. return 0;
  208. }
  209. }
  210. function sql_insert_id ()
  211. {
  212. switch ($this->dbs_type)
  213. {
  214. case "MySQL":
  215. $value = mysql_insert_id($this->conn);
  216. break;
  217. case "MSSQL":
  218. $erg = @mssql_query("SELECT MAX(id) FROM {$this->table}",$this->conn);
  219. $value = mssql_result($this->conn,0,0);
  220. break;
  221. }
  222. return $value;
  223. }
  224. function sql_error ()
  225. {
  226. $t_query = end($this->sql_query);
  227. return $t_query[4];
  228. }
  229. function sql_errno ()
  230. {
  231. $t_query = end($this->sql_query);
  232. return $t_query[3];
  233. }
  234. function sql_fields ($id)
  235. {
  236. $id -= 1;
  237. $sql = $this->sql_query[$id][0];
  238. $erg = mysql_query($sql);
  239. $i = 0;
  240. while ($i < mysql_num_fields($erg))
  241. {
  242. $fields[$i] = mysql_fetch_field($erg, $i);
  243. $fieldset[$fields[$i]->table][$fields[$i]->name] = $i;
  244. $i++;
  245. }
  246. foreach ($fieldset as $t_table => $t_fields)
  247. {
  248. $sql2 = "SHOW COLUMNS FROM {$t_table}";
  249. $erg2 = mysql_query($sql2);
  250. while ($k = mysql_fetch_assoc($erg2))
  251. {
  252. if (isset($t_fields[$k['Field']]))
  253. {
  254. $k['id'] = $fieldset[$t_table][$k['Field']];
  255. if ("" != $k['Default'])
  256. {
  257. $t_default = "DEFAULT '{$k['Default']}'";
  258. }
  259. else
  260. {
  261. $t_default = "";
  262. }
  263. if ("" == $k['Null']) $k['Null'] = "NOT NULL";
  264. $k['Row'] = "`{$k['Field']}` {$k['Type']} {$k['Null']} {$t_default}";
  265. $fieldset[$t_table][$k['Field']] = $k;
  266. }
  267. }
  268. }
  269. $this->sql_fields_anzeigen ($fieldset,$sql);
  270. return ($fieldset);
  271. }
  272. function sql_fields_anzeigen ($fieldset,$insert_query)
  273. {
  274. $number = rand(10000,99999);
  275. $create_query = "CREATE TABLE `temp_{$number}` (\n";
  276. foreach ($fieldset as $t_table => $t_fields)
  277. {
  278. foreach ($t_fields as $t_field)
  279. {
  280. $rows[$t_field['id']] = $t_field['Row'];
  281. }
  282. }
  283. $create_query .= implode(",\n",$rows) . "\n";
  284. $create_query .= ") TYPE=MyISAM ;";
  285. $insert_query = "INSERT INTO `temp_{$number}`\n" . $insert_query;
  286. echo "<div><pre>" . $create_query . "\n\n" . $insert_query . "\n\n</pre></div>";
  287. }
  288. function sqlbox_anzeigen ()
  289. {
  290. array_shift($this->error);
  291. $this->error = array_filter($this->error);
  292. $sqlbox = "<b>" . count($this->error) . "</b><br>";
  293. foreach ($this->sql_query as $id => $info)
  294. {
  295. $sqlbox .= "#" . ($id+1) . ":<br>";
  296. $info[0] = nl2br($info[0]);
  297. if (0 == $info[3])
  298. {
  299. $sqlbox .= " <font style='color:#000080;'>{$info[0]};</font><br>";
  300. if (0 < $info[1])
  301. {
  302. $sqlbox .= " Datens&auml;tze: " . $info[1] . "<br>";
  303. }
  304. elseif (0 < $info[2])
  305. {
  306. $sqlbox .= " Betroffene Datens&auml;tze: " . $info[2] . "<br>";
  307. }
  308. }
  309. else
  310. {
  311. $sqlbox .= " <font style='color:#800000;'>{$info[0]};</font><br>";
  312. $sqlbox .= " <b>" . $info[3] . "</b>: " . $info[4] . "<br>";
  313. }
  314. $sqlbox .= " Dauer: " . $info[5] . "<br><br>\n";
  315. }
  316. include (TEMPLATE_DIR."template.sql.inc");
  317. }
  318. }
  319. ?>