PHP database and Operation instances

Source: Internet
Author: User
Tags pconnect
PHP database and Operation instances are not very powerful, but they are quite convenient to use.

PHP database and Operation instances are not very powerful, but they are quite convenient to use.
Db. class. php

PHP code
  1. /**
  2. * Database
  3. */
  4. Class DataBase
  5. {
  6. Var $ pConnect = FALSE; // whether to use persistent connections
  7. Var $ mHost; // database host
  8. Var $ mDatabase;
  9. Var $ db; // Database
  10. Var $ mUser; // database username
  11. Var $ mPwd; // database user password
  12. Var $ mConn; // Connection ID
  13. Var $ result; // result resource ID for executing the query command
  14. Var $ num_rows; // number of returned entries
  15. Var $ insert_id; // returns the ID of the last INSERT command.
  16. Var $ affected_rows; // number of columns affected by the query command returned
  17. // The number of columns (row) affected by INSERT, UPDATE, or DELETE.
  18. // If delete does not contain the where clause, 0 is returned.
  19. // Constructor
  20. Public function _ construct ($ host, $ user, $ pwd, $ db)
  21. {
  22. $ This-> mHost = $ host;
  23. $ This-> mUser = $ user;
  24. $ This-> mPwd = $ pwd;
  25. $ This-> db = $ db;
  26. }
  27. // Database connection
  28. Public function connect ()
  29. {
  30. If ($ this-> pConnect)
  31. $ This-> mConn = mysql_pconnect ($ this-> mHost, $ this-> mUser, $ this-> mPwd); // persistent connection
  32. Else
  33. $ This-> mConn = mysql_connect ($ this-> mHost, $ this-> mUser, $ this-> mPwd); // short connect
  34. If (! $ This-> mConn) $ this-> dbhalt ("cannot connect to the database! ");
  35. If ($ this-> db = "") $ this-> db = $ this-> dbDatabase;
  36. If (! Mysql_select_db ($ this-> db, $ this-> mConn ))
  37. $ This-> dbhalt ("The database is unavailable! ");
  38. } // Eof # dbconnect ()
  39. // Change the database
  40. Public function dbChange ($ db ){
  41. $ This-> db = $ db;
  42. $ This-> connect ();
  43. }
  44. // Execute the SQL statement and return the result resource id
  45. Public function execute ($ SQL ){
  46. $ This-> result = mysql_query ($ SQL );
  47. Return $ this-> result;
  48. }
  49. // Retrieve array-index and association
  50. Public function fetchArray ($ resultType = MYSQL_BOTH)
  51. {
  52. Return mysql_fetch_array ($ this-> result, $ resultType );
  53. }
  54. // Obtain the associated array
  55. Public function fetchAssoc ()
  56. {
  57. Return mysql_fetch_assoc ($ this-> result );
  58. }
  59. // Obtain the number index array
  60. Public function fetchIndexArray ()
  61. {
  62. Return mysql_fetch_row ($ this-> result );
  63. }
  64. // Obtain the object array
  65. Public function fetchObject ()
  66. {
  67. Return mysql_fetch_object ($ this-> result );
  68. }
  69. // Number of returned Records
  70. Function numRows ()
  71. {
  72. Return mysql_num_rows ($ this-> result );
  73. }
  74. // Return all database names in the host
  75. Public function dbNames ()
  76. {
  77. $ RsPtr = mysql_list_dbs ($ this-> mConn );
  78. $ I = 0;
  79. $ Cnt = mysql_num_rows ($ rsPtr );
  80. While ($ I <$ cnt)
  81. {
  82. $ Rs [] = mysql_db_name ($ rsPtr, $ I );
  83. $ I ++;
  84. }
  85. Return $ rs;
  86. }
  87. Function dbhalt ($ errmsg ){
  88. $ Msg = "database error! ";
  89. $ Msg = $ errmsg;
  90. Echo "$ msg ";
  91. Die ();
  92. }
  93. // Delete
  94. Function delete ($ SQL ){
  95. $ Result = $ this-> execute ($ SQL, $ dbbase );
  96. $ This-> affected_rows = mysql_affected_rows ($ this-> dbLink );
  97. $ This-> free_result ($ result );
  98. Return $ this-> affected_rows;
  99. }
  100. // Add
  101. Function insert ($ SQL ){
  102. $ Result = $ this-> execute ($ SQL, $ dbbase );
  103. $ This-> insert_id = mysql_insert_id ($ this-> dbLink );
  104. $ This-> free_result ($ result );
  105. Return $ this-> insert_id;
  106. }
  107. // Modify
  108. Function update ($ SQL ){
  109. $ Result = $ this-> execute ($ SQL, $ dbbase );
  110. $ This-> affected_rows = mysql_affected_rows ($ this-> dbLink );
  111. $ This-> free_result ($ result );
  112. Return $ this-> affected_rows;
  113. }
  114. // Close the connection
  115. Function dbclose (){
  116. Mysql_close ($ this-> dbLink );
  117. }
  118. } // End class
  119. ?>

Directly reference:

 

PHP code
  1. Include "db. class. php ";
  2. $ Mydb = new DataBase ("localhost", "root", "123456", "test ");
  3. $ Mydb-> connect ();
  4. $ Mydb-> execute ("set names GBK ");
  5. $ Mydb-> execute ("select * from usrs ");
  6. Print_r ($ mydb-> dbNames ());
  7. ?>

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.