Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: klasa SQL
Forum PHP.pl > Forum > PHP
MarWWWa
Czy ma ktoś może przykładową klase do baz sql?? chodzi mi o to zeby nie trzeba bylo pisac za kazdym razem
  1. mysql_query
czy tez
  1. mysql_num_rows
bo probowalem cos takiego zrobic ale np. jak zrobie w klasie funkcje do polaczenia z baza to ni wywietla mi bledow mimo tego ze nie podalem w skladni
  1. mysql_connect
zadnych danych
hwao
  1. <?php
  2. // connect
  3. function connect ( $h, $u, $p, $b )
  4. {
  5. mysql_connect( $h, $u, $b );
  6. }
  7. //query
  8. function query( $sql )
  9. {
  10.  return mysql_query( $sql );
  11. }
  12. ?>

Zrob sobie to tak mniejwiecej.
Rozbuduj do wlasnych potrzeb
DeyV
ADoDB smile.gif (adres w stopce forum)
radzaw
albo DB z PEAR'a smile.gif
sf
Jak Ci nie wyswietla bledow? tongue.gif To spraw by sie wyswietlaly winksmiley.jpg

http://pl2.php.net/manual/pl/function.mysql-error.php
Riklaunim
ezSQL - abstrakcyjna baza danych dla MySQL, Postresql, SQLite i innych smile.gif
XeqtR
  1. <?php
  2.  
  3. class Db {
  4. var $no_error = 0;
  5. var $connection;
  6. var $query_id = 0;
  7. var $query_count = 0;
  8. var $query_time = 0;
  9. var $query_array = array();
  10. var $table_fields = array();
  11.  
  12. function Db($db_host, $db_user, $db_password = &#092;"\", $db_name = \"\", $db_pconnect = 0) {
  13. $connect_handle = ($db_pconnect) ? &#092;"mysql_pconnect\" : \"mysql_connect\";
  14. if (!$this->connection = $connect_handle($db_host, $db_user, $db_password)) {
  15. $this->error(&#092;"Could not connect to the database server ($db_host, $db_user).\", 1);
  16. }
  17. if ($db_name != &#092;"\") {
  18. if (!@mysql_select_db($db_name)) {
  19. @mysql_close($this->connection);
  20. $this->error(&#092;"Could not select database ($db_name).\", 1);
  21. }
  22. }
  23. return $this->connection;
  24. }
  25.  
  26. function close() {
  27. if ($this->connection) {
  28. if ($this->query_id) {
  29. @mysql_free_result($this->query_id);
  30. }
  31. return @mysql_close($this->connection);
  32. }
  33. else {
  34. return false;
  35. }
  36. }
  37.  
  38. function query($query = &#092;"\") {
  39. unset($this->query_id);
  40. if ($query != &#092;"\") {
  41. if ((defined(&#092;"PRINT_QUERIES\") && PRINT_QUERIES == 1) || (defined(\"PRINT_STATS\") && PRINT_STATS == 1)) {
  42. $startsqltime = explode(&#092;" \", microtime());
  43. }
  44. if (!$this->query_id = @mysql_query($query, $this->connection)) {
  45. $this->error(&#092;"<b>Bad SQL Query</b>: \".htmlentities($query).\"<br /><b>\".mysql_error().\"</b>\");
  46. }
  47. if ((defined(&#092;"PRINT_QUERIES\") && PRINT_QUERIES == 1) || (defined(\"PRINT_STATS\") && PRINT_STATS == 1)) {
  48. $endsqltime = explode(&#092;" \", microtime());
  49. $totalsqltime = round($endsqltime[0]-$startsqltime[0]+$endsqltime[1]-$startsqltime[1],3);
  50. $this->query_time += $totalsqltime;
  51. $this->query_count++;
  52. }
  53. if (defined(&#092;"PRINT_QUERIES\") && PRINT_QUERIES == 1) {
  54. $query_stats = htmlentities($query);
  55. $query_stats .= &#092;"<br><b>Querytime:</b> \".$totalsqltime;
  56. $this->query_array[] = $query_stats;
  57. }
  58. return $this->query_id;
  59. }
  60. }
  61.  
  62. function fetch_array($query_id = -1, $assoc = 0) {
  63. if ($query_id != -1) {
  64. $this->query_id = $query_id;
  65. }
  66. if ($this->query_id) {
  67. return ($assoc) ? mysql_fetch_assoc($this->query_id) : mysql_fetch_array($this->query_id);
  68. }
  69. }
  70.  
  71. function free_result($query_id = -1) {
  72. if ($query_id != -1) {
  73. $this->query_id = $query_id;
  74. }
  75. return @mysql_free_result($this->query_id);
  76. }
  77.  
  78. function query_firstrow($query = &#092;"\") {
  79. if ($query != &#092;"\") {
  80. $this->query($query);
  81. }
  82. $result = $this->fetch_array($this->query_id);
  83. $this->free_result();
  84. return $result;
  85. }
  86.  
  87. function get_numrows($query_id = -1) {
  88. if ($query_id != -1) {
  89. $this->query_id = $query_id;
  90. }
  91. return mysql_num_rows($this->query_id);
  92. }
  93.  
  94. function get_insert_id() {
  95. return ($this->connection) ? @mysql_insert_id($this->connection) : 0;
  96. }
  97.  
  98. function get_next_id($column = &#092;"\", $table = \"\") {
  99. if (!empty($column) && !empty($table)) {
  100. $sql = &#092;"SELECT MAX($column) AS max_id
  101. FROM $table&#092;";
  102. $row = $this->query_firstrow($sql);
  103. return (($row['max_id'] + 1) > 0) ? $row['max_id'] + 1 : 1;
  104. }
  105. else {
  106. return NULL;
  107. }
  108. }
  109.  
  110. function get_numfields($query_id = -1) {
  111. if ($query_id != -1) {
  112. $this->query_id = $query_id;
  113. }
  114. return @mysql_num_fields($this->query_id);
  115. }
  116.  
  117. function get_fieldname($query_id = -1, $offset) {
  118. if ($query_id != -1) {
  119. $this->query_id = $query_id;
  120. }
  121. return @mysql_field_name($this->query_id, $offset);
  122. }
  123.  
  124. function get_fieldtype($query_id = -1, $offset) {
  125. if ($query_id != -1) {
  126. $this->query_id = $query_id;
  127. }
  128. return @mysql_field_type($this->query_id, $offset);
  129. }
  130.  
  131. function affected_rows() {
  132. return ($this->connection) ? @mysql_affected_rows($this->connection) : 0;
  133. }
  134.  
  135. function is_empty($query = &#092;"\") {
  136. if ($query != &#092;"\") {
  137. $this->query($query);
  138. }
  139. return (!mysql_num_rows($this->query_id)) ? 1 : 0;
  140. }
  141.  
  142. function not_empty($query = &#092;"\") {
  143. if ($query != &#092;"\") {
  144. $this->query($query);
  145. }
  146. return (!mysql_num_rows($this->query_id)) ? 0 : 1;
  147. }
  148.  
  149. function get_table_fields($table) {
  150. if (!empty($this->table_fields[$table])) {
  151. return $this->table_fields[$table];
  152. }
  153. $this->table_fields[$table] = array();
  154. $result = $this->query(&#092;"SHOW FIELDS FROM $table\");
  155. while ($row = $this->fetch_array($result)) {
  156. $this->table_fields[$table][$row['Field']] = $row['Type'];
  157. }
  158. return $this->table_fields[$table];
  159. }
  160.  
  161. function error($errmsg, $halt = 0) {
  162. if (!$this->no_error) {
  163. echo &#092;"<br /><font color='#FF0000'><b>DB Error</b></font>: \".$errmsg.\"<br />\";
  164. if ($halt) {
  165. }
  166. }
  167. }
  168. } // end of class
  169. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.