A database operation PHP class

Source: Internet
Author: User
  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; The result resource ID of the Execute Query command
  13. Private $sql; SQL execution Statements
  14. Private $pre; database table prefixes
  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 database link configuration!") "));
  28. if (!mysql_select_db ($this->db_table, $this->db_conn)) {
  29. echo "No data table 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 statement */
  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 result set bars based on 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 server for all databases */
  61. Separate the system database from 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. Returns all database names in the host as an array
  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 is case sensitive with $row->content fields
  103. Mysql_fetch_object () object with $row[id], $row [content] field is case sensitive
  104. */
  105. /* Get Recordset, get array-Index and association, use $row[' content ' */
  106. Public Function Fetch_array ()
  107. {
  108. Return @mysql_fetch_array ($this->result);
  109. }
  110. Get associative array, use $row[' field name ']
  111. Public Function Fetch_ass ()
  112. {
  113. Return @mysql_fetch_assoc ($this->result);
  114. }
  115. Get a numeric index array, using $row[0], $row [1], $row [2]
  116. Public Function Fetch_row ()
  117. {
  118. Return @mysql_fetch_row ($this->result);
  119. }
  120. Get an array of objects, using $row->content
  121. Public Function Fetch_object ()
  122. {
  123. Return @mysql_fetch_object ($this->result);
  124. }
  125. Simplify 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. Simplified 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 ("Please enter the parameter array! ");
  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 ("Conditions 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. Gets the ID generated by the INSERT operation in the previous step
  181. Public Function insert_id () {
  182. return mysql_insert_id ();
  183. }
  184. Data table with prefix
  185. Public Function Fulltablename ($table) {
  186. return $table = $this->pre. $table;
  187. }
  188. Number of query 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. ";
  200. echo "
    ";
  201. }
  202. Get MySQL Server information
  203. Public Function mysql_server ($num = ") {
  204. Switch ($num) {
  205. Case 1:
  206. return Mysql_get_server_info (); MySQL Server information
  207. Break
  208. Case 2:
  209. return Mysql_get_host_info (); Get MySQL host Information
  210. Break
  211. Case 3:
  212. return Mysql_get_client_info (); Get MySQL Client Information
  213. Break
  214. Case 4:
  215. return Mysql_get_proto_info (); Get MySQL protocol Information
  216. Break
  217. Default
  218. return Mysql_get_client_info (); MySQL version information is obtained by default
  219. }
  220. }
  221. destructor, automatically shuts down the database, garbage collection mechanism
  222. /*public function __destruct ()
  223. {
  224. if (!empty ($this->result)) {
  225. $this->free ();
  226. }
  227. Mysql_close ($this, $db _conn);
  228. }*/
  229. /* Get the client's real IP address */
  230. function GetIP () {
  231. if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "Unknown")
  232. {
  233. $ip = getenv ("Http_client_ip");
  234. }
  235. else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown")) {
  236. $ip = getenv ("Http_x_forwarded_for");
  237. }
  238. else if (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "Unknown")
  239. {
  240. $ip = getenv ("REMOTE_ADDR");
  241. }
  242. else if (isset ($_server[' remote_addr ')) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], "unknown") {
  243. $ip = $_server[' remote_addr ');
  244. }
  245. else{
  246. $ip = "Unknown";
  247. }
  248. return ($IP);
  249. }
  250. function Show_error ($STR) {
  251. echo "";
  252. }
  253. }
  254. ?>
Copy Code
Php
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.