Database Connection operations

Source: Internet
Author: User
Database Connection operations
Database Connection operations

  1. Class mysql
  2. {
  3. Private $ _ link;
  4. Public function _ construct ($ dbhost, $ dbuser, $ dbpassword, $ dbname, $ charset = 'utf8 ')
  5. {
  6. $ This-> _ link = mysql_connect ($ dbhost, $ dbuser, $ dbpassword, true);/* connect to database */
  7. $ This-> _ link or $ this-> errmsg ('Can \'t connect to MySQL server! ');/* Whether the connection is successful */
  8. If ($ this-> version ()> '4. 1') {/* check the database version */
  9. $ This-> query ('set names'. $ charset);/* set Database encoding */
  10. }
  11. Mysql_select_db ($ dbname, $ this-> _ link) or $ this-> errmsg ('Can \'t select the database! ');/* Open the database */
  12. }
  13. /* Perform database operations */
  14. Public function query ($ SQL)
  15. {
  16. $ Result = mysql_query ($ SQL, $ this-> _ link );
  17. $ Result or $ this-> errmsg ('execute SQL sentence error! ');
  18. Return $ result;
  19. }
  20. /* Return the array generated based on the row obtained from the result set */
  21. /* MYSQL_BOTH get an array containing both join and numeric indexes (like mysql_fetch_array ())*/
  22. /* MYSQL_ASSOC: get an array containing both join and digital indexes (like mysql_fetch_assoc ())*/
  23. /* MYSQL_NUM get an array containing both join and numeric indexes (like mysql_fetch_row ())*/
  24. Public function fetch_array ($ result, $ type = MYSQL_BOTH)
  25. {
  26. Return mysql_fetch_array ($ result, $ type );
  27. }
  28. /* Return the object generated based on the obtained row */
  29. Public function fetch_object ($ result)
  30. {
  31. Return mysql_fetch_object ($ result );
  32. }
  33. /* Obtain the number of rows affected by the previous MySQL operation */
  34. Public function affected_rows ()
  35. {
  36. Return mysql_affected_rows ($ this-> _ link );
  37. }
  38. /* Release the result memory */
  39. Public function free_result ($ result)
  40. {
  41. Return mysql_free_result ($ result );
  42. }
  43. /* Number of rows in the result set */
  44. Public function num_rows ($ result)
  45. {
  46. Return mysql_num_rows ($ result );
  47. }
  48. /* Number of fields in the result set */
  49. Public function num_fields ($ result)
  50. {
  51. Return mysql_num_fields ($ result );
  52. }
  53. /* Obtain the ID generated by the previous INSERT operation */
  54. Public function insert_id ()
  55. {
  56. Return mysql_insert_id ($ this-> _ link );
  57. }
  58. /* Issue a mysql execution error */
  59. Private function errmsg ($ msg)
  60. {
  61. $ Message ='A mysql error has occurred!
    ';
  62. $ Message. ='Error Number:'. Mysql_errno ($ this-> _ link ).'
    ';
  63. $ Message. ='Error Description:'. $ Msg. mysql_error ($ this-> _ link ).'
    ';
  64. $ Message. ='Error Time:'. Date ('Y-m-d H: I: s ');
  65. Exit ($ message );
  66. }
  67. /* Return the connection id */
  68. Public function link_id ()
  69. {
  70. Return $ this-> _ link;
  71. }
  72. /* Return the database server version */
  73. Public function version (){
  74. Return mysql_get_server_info ($ this-> _ link );
  75. }
  76. /* Obtain the real IP address of the client */
  77. Function getip (){
  78. If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown ")){
  79. $ Ip = getenv ("HTTP_CLIENT_IP ");
  80. } Else
  81. If (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")){
  82. $ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
  83. } Else
  84. If (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown ")){
  85. $ Ip = getenv ("REMOTE_ADDR ");
  86. } Else
  87. If (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")){
  88. $ Ip = $ _ SERVER ['remote _ ADDR '];
  89. } Else {
  90. $ Ip = "unknown ";
  91. }
  92. Return ($ ip );
  93. }
  94. }

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.