A database operation PHP class

WBOY
Release: 2016-07-25 08:44:27
Original
1010 people have browsed it
  1. /*
  2. * Author Molong
  3. * Time December 2, 2010 15:50:35
  4. */
  5. $db = new mysql($db_host,$db_user,$db_password, $db_table,$db_conn,$pre,$coding);
  6. class mysql{
  7. private $db_host;
  8. private $db_user;
  9. private $db_password;
  10. private $db_table;
  11. private $db_conn; //Database connection identification;
  12. private $result; //Result resource identification of executing query command
  13. private $sql; //SQL execution statement
  14. private $pre; //Database table prefix
  15. private $coding; //Database encoding, GBK, UTF8, gb2312
  16. function __construct($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding){
  17. $this->db_host = $db_host;
  18. $this->db_user = $db_user;
  19. $this->db_password = $db_password;
  20. $this->db_table = $db_table;
  21. $this->db_conn = $db_conn;
  22. $this->pre = $pre;
  23. $this->coding = $coding;
  24. $this->connect();
  25. }
  26. function connect(){
  27. $this->db_conn = @mysql_connect($this->db_host,$this->db_user, $this->db_password) or die($this->show_error("Database link error, please check the database link configuration! "));
  28. if(!mysql_select_db($this->db_table,$this->db_conn)){
  29. echo "Data table not found: ".$this->db_table;
  30. }
  31. mysql_select_db($ this->db_table,$this->db_conn);
  32. $this->query("SET NAMES $this->coding");
  33. }
  34. /*Function to execute SQL statements*/
  35. function query ($sql){
  36. if(emptyempty($sql)){
  37. $this->show_error("Your sql statement cannot be empty!");
  38. }else{
  39. $this->sql = $sql;
  40. }
  41. $result = mysql_query($this->sql,$this->db_conn);
  42. return $this->result = $result;
  43. }
  44. /*Create and add a new database*/
  45. public function create_database($database_name){
  46. $database=$database_name;
  47. $sqlDatabase = 'create database '.$database;
  48. return $this- >query($sqlDatabase);
  49. }
  50. // Calculate the number of result set items based on the select query results
  51. public function db_num_rows(){
  52. if($this->result==null){
  53. if($this- >show_error){
  54. $this->show_error("sql statement error!");
  55. }
  56. }else{
  57. return mysql_num_rows($this->result);
  58. }
  59. }
  60. /*Query all server Database*/
  61. //Separate the system database and the user database for a more intuitive display?
  62. public function show_databases(){
  63. $this->query("show databases");
  64. echo "Existing database:".$ amount =$this->db_num_rows($rs);
  65. echo "
    ";
  66. $i=1;
  67. while($row = $this->fetch_array($rs)){
  68. echo "$i $row[Database]";
  69. echo "
    ";
  70. $i++;
  71. }
  72. }
  73. //Return all database names in the host in array form
  74. public function databases()
  75. {
  76. $rsPtr=mysql_list_dbs($this->db_conn);
  77. $i=0;
  78. $cnt=mysql_num_rows($rsPtr);
  79. while($i<$cnt)
  80. {
  81. $rs[]=mysql_db_name($ rsPtr,$i);
  82. $i++;
  83. }
  84. return print_r($rs);
  85. }
  86. /*Query all tables under the database*/
  87. function show_tables($database_name){
  88. $this->query( "show tables");
  89. echo "Existing database:".$amount = $this->db_num_rows($rs);
  90. echo "
    ";
  91. $i=1;
  92. while($ row = $this->fetch_array($rs)){
  93. $columnName="Tables_in_".$database_name;
  94. echo "$i $row[$columnName]";
  95. echo "
    ";
  96. $i++;
  97. }
  98. }
  99. /*
  100. mysql_fetch_row() array $row[0],$row[1],$row[2]
  101. mysql_fetch_array() array $row[0] or $row[id]
  102. mysql_fetch_assoc() array uses $row->content field case sensitivity
  103. mysql_fetch_object() object uses $row[id],$row[content] fields case sensitivity
  104. */
  105. /*Get the record set, get the array-index and association, use $row['content'] */
  106. public function fetch_array()
  107. {
  108. return @mysql_fetch_array($this->result);
  109. }
  110. //Get the association array, use $row[' field name']
  111. public function fetch_ass()
  112. {
  113. return @mysql_fetch_assoc($this->result);
  114. }
  115. //Get the numeric index array, use $row[0],$row[1],$row [2]
  116. public function fetch_row()
  117. {
  118. return @mysql_fetch_row($this->result);
  119. }
  120. //Get the object array, use $row->content
  121. public function fetch_Object()
  122. {
  123. return @mysql_fetch_object($this->result);
  124. }
  125. //Simplified query select
  126. public function findall($table){
  127. $table = $this->fulltablename($table);
  128. $this-> ;query("select * from $table");
  129. }
  130. public function select($table,$columnName,$condition){
  131. $table = $this->fulltablename($table);
  132. if(emptyempty( $columnName)){
  133. $columnName = "*";
  134. }
  135. $this->query("SELECT $columnName FROM $table $condition");
  136. }
  137. //Simplified insert
  138. function insert($table ,$arr){
  139. $table = $this->fulltablename($table);
  140. $sql = "INSERT INTO $table ";
  141. if(!is_array($arr)){
  142. $this->show_error( "Please enter the parameter array!");
  143. }else{
  144. $k = "";
  145. $v = "";
  146. foreach($arr as $key => $value){
  147. $k .= "`$key`,";
  148. $v .= "'".$value."',";
  149. }
  150. }
  151. $sql = $sql." (".substr($k,0,-1).") VALUES (".substr($v,0,-1).")";
  152. $this->query($sql);
  153. }
  154. //简化的update
  155. function update($table,$arr,$where){
  156. $table = $this->fulltablename($table);
  157. $sql = "UPDATE $table SET ";
  158. if(!is_array($arr)){
  159. $this->show_error("请输入参数数组!");
  160. }else{
  161. foreach($arr as $key => $value){
  162. $sql .= " `".$key."` = '".$value."' ,";
  163. }
  164. }
  165. $sql = substr($sql,0,-1)." where ".$where;
  166. return $this->query($sql);
  167. }
  168. //Simplified delete
  169. function delete($ table,$where = ""){
  170. $table = $this->fulltablename($table);
  171. if(emptyempty($where)){
  172. $this->show_error("Condition cannot be empty!") ;
  173. }else{
  174. $where = " where ".$where;
  175. }
  176. $sql = "DELETE FROM $table ".$where;
  177. //echo $sql;
  178. return $this->query($sql );
  179. }
  180. //Get the ID generated by the previous INSERT operation
  181. public function insert_id(){
  182. return mysql_insert_id();
  183. }
  184. //Prefixed data table
  185. public function fulltablename($table){
  186. return $table = $this->pre.$table;
  187. }
  188. //Query the number of fields
  189. public function num_fields($table){
  190. $table = $this->fulltablename($table);
  191. $ this->query("select * from $table");
  192. echo "
    ";
  193. echo "Number of fields: ".$total = mysql_num_fields($this->result);
  194. echo "
    "; 
  195. for ($i=0; $i<$total; $i++){
  196. print_r(mysql_fetch_field($this->result,$i) );
  197. }
  198. echo "
  199. echo "
    ";
  200. }
  201. //Get MySQL server information
  202. public function mysql_server($num=''){
  203. switch ($num){
  204. case 1:
  205. return mysql_get_server_info (); //MySQL server information
  206. break;
  207. case 2 :
  208. return mysql_get_host_info(); //Get MySQL host information
  209. break;
  210. case 3 :
  211. return mysql_get_client_info(); //Get MySQL client information
  212. break;
  213. case 4:
  214. return mysql_get_proto_info(); //Get MySQL protocol information
  215. break;
  216. default:
  217. return mysql_get_client_info(); //Get mysql version information by default
  218. }
  219. }
  220. //Destructor , automatically close the database, garbage collection mechanism
  221. /*public function __destruct()
  222. {
  223. if(!empty($this->result)){
  224. $this->free();
  225. }
  226. mysql_close($this ->$db_conn);
  227. }*/
  228. /*Get the real IP address of the client*/
  229. function getip(){
  230. if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), " unknown"))
  231. {
  232. $ip = getenv("HTTP_CLIENT_IP");
  233. }
  234. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
  235. $ip = getenv ("HTTP_X_FORWARDED_FOR");
  236. }
  237. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  238. {
  239. $ip = getenv("REMOTE_ADDR");
  240. }
  241. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
  242. $ip = $_SERVER['REMOTE_ADDR'];
  243. }
  244. else{
  245. $ip = "unknown";
  246. }
  247. return($ip);
  248. }
  249. function show_error($str){
  250. echo "";
  251. }
  252. }
  253. ?>
Copy code

PHP


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template