DB Connect Fetch Data


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**************************************
  3.  seesaw associates | http://seesaw.net
  4.  
  5.  client: mysql
  6.  file: class.mysql.php
  7.  description: handles mysql paging
  8.  
  9.  Copyright (C) 2008 Matt Kenefick(.com)
  10. **************************************/
  11.  
  12. class DB{
  13. var $host;
  14. var $user_name;
  15. var $password;
  16. var $db_name;
  17.  
  18. var $link_id;
  19. var $result;
  20. var $col;
  21. var $query;
  22. var $fields;
  23. var $records;
  24. var $setting;
  25.  
  26. var $debug = false;
  27. var $query_count = 0;
  28. var $debug_file = "debug.sql";
  29.  
  30. function settings($key,$value){
  31. $this->setting[$key] = $value;
  32. }
  33.  
  34. function init($_host, $_user, $_password, $_db_name){
  35. $this->host = $_host;
  36. $this->user_name = $_user;
  37. $this->password = $_password;
  38. $this->db_name = $_db_name;
  39. $this->fields = array();
  40.  
  41. $this->link_id = @mysql_connect($_host, $_user, $_password) or die("Your website is not properly installed.");
  42. @mysql_select_db($_db_name, $this->link_id);
  43. }
  44.  
  45. function assign($field, $value){
  46. $this->fields[$field] = ($value)==""?("'".$value."'"):$value;
  47. }
  48. function assign_str($field, $value){
  49. $this->fields[$field] = "'".AddSlashes($value)."'";
  50. //$this->fields[$field] = "'".($value)."'";
  51. }
  52.  
  53. function reset(){
  54. $this->fields = array();
  55. }
  56. function insert($table){
  57. $f = "";
  58. $v = "";
  59. reset($this->fields);
  60. foreach($this->fields as $field=>$value){
  61. $f.= ($f!=""?", ":"").$field;
  62. $v.= ($v!=""?", ":"").$value;
  63. }
  64. $sql = "INSERT INTO ".$table." (".$f.") VALUES (".$v.")";
  65. $this->query($sql);
  66. return $this->insert_id();
  67. }
  68.  
  69. function update($table, $where){
  70. $f = "";
  71. reset($this->fields);
  72. foreach($this->fields as $field=>$value){
  73. $f.= ($f!=""?", ":"").$field." = ".$value;
  74. }
  75. $sql = "UPDATE ".$table." SET ".$f." ".$where;
  76. $this->query($sql);
  77. }
  78.  
  79. function timestampFormat($unixNumber){
  80. return date('Y-m-d H:i:s',$unixNumber);
  81. /// xxxx-xx-xx xx-xx-xx
  82. }
  83.  
  84. function query($_query){
  85. list($usec, $sec) = explode(" ",microtime());
  86. $time_start = ((float)$usec + (float)$sec);
  87.  
  88. $this->query = $_query;
  89. $this->result = @mysql_query($_query, $this->link_id) or die( $_query."<p>".mysql_error($this->link_id) );
  90.  
  91. list($usec, $sec) = explode(" ",microtime());
  92. $time_end = ((float)$usec + (float)$sec);
  93. $time = $time_end - $time_start;
  94.  
  95. if($this->debug){
  96. $this->query_count ++;
  97. $f = fopen($this->debug_file, "a");
  98. $sss = "# ".$this->query_count."\n ".$time." sec \n\n".$_query."\n#-------------------------------------------------------------------------\n\n";
  99. fputs($f, $sss, strlen($sss));
  100. fclose($f);
  101. }
  102.  
  103. return $this->result;
  104. }
  105.  
  106. function get_records(){
  107. $this->records = array();
  108. while($row = @mysql_fetch_array($this->result, MYSQL_BOTH)){
  109. $this->records[count($this->records)] = $row;
  110. }
  111. reset($this->records);
  112. return $this->records;
  113. }
  114.  
  115. function get_tables_status(){
  116. $this->query("SHOW TABLE STATUS FROM `".$this->db_name."`");
  117. if($this->num_rows() > 0){
  118. $tables = array();
  119. while($this->movenext()){
  120. $tables[$this->col["Name"]] = $this->col;
  121. }
  122. return $tables;
  123. }
  124. return false;
  125. }
  126.  
  127. function fetch_array(){
  128. $this->col = @mysql_fetch_array($this->result, MYSQL_BOTH);
  129. }
  130.  
  131. function num_rows(){
  132. return (int)@mysql_num_rows($this->result);
  133. }
  134.  
  135. function fixSlashes(){
  136. if($this->col){
  137. foreach($this->col as $key => $value)
  138. $this->col[$key] = StripSlashes($value);
  139. return $this->col;
  140. }
  141. }
  142.  
  143. function movenext(){
  144. $this->col=@mysql_fetch_array($this->result, MYSQL_ASSOC);
  145. if($this->setting['fixSlashes'])
  146. return $this->fixSlashes();
  147. else
  148. return $this->col;
  149. }
  150.  
  151. function done(){
  152. @mysql_close($this->link_id);
  153. }
  154.  
  155. function insert_id(){
  156. return @mysql_insert_id($this->link_id);
  157. }
  158.  
  159. function affected_rows(){
  160. return @mysql_affected_rows($this->link_id);
  161. }
  162. }
  163. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.