PHP database operation base class (Singleton mode)-php Tutorial

Source: Internet
Author: User
PHP database operation base class (Singleton mode)
Database operation base classes written by yourself, including the most basic CURD operations, which can be integrated into the framework.

  1. // Configuration file
  2. $ Db = array (
  3. 'Host' => 'localhost ',
  4. 'User' => 'root ',
  5. 'Password' => '',
  6. 'Database' => 'test ',
  7. )
  8. ?>
  9. // Php class
  10. Class db {
  11. Public $ conn;
  12. Public static $ SQL;
  13. Public static $ instance = null;
  14. Private function _ construct (){
  15. Require_once ('Db. config. php ');
  16. $ This-> conn = mysql_connect ($ db ['host'], $ db ['user'], $ db ['password']);
  17. If (! Mysql_select_db ($ db ['database'], $ this-> conn )){
  18. Echo "failed ";
  19. };
  20. Mysql_query ('set names utf8', $ this-> conn );
  21. }
  22. Public static function getInstance (){
  23. If (is_null (self: $ instance )){
  24. Self: $ instance = new db;
  25. }
  26. Return self: $ instance;
  27. }
  28. /**
  29. * Query a database
  30. */
  31. Public function select ($ table, $ condition = array (), $ field = array ()){
  32. $ Where = '';
  33. If (! Empty ($ condition )){
  34. Foreach ($ condition as $ k => $ v ){
  35. $ Where. = $ k. "= '". $ v. "' and ";
  36. }
  37. $ Where = 'where'. $ where. '1 = 1 ';
  38. }
  39. $ Fieldstr = '';
  40. If (! Empty ($ field )){
  41. Foreach ($ field as $ k => $ v ){
  42. $ Fieldstr. = $ v .',';
  43. }
  44. $ Fieldstr = rtrim ($ fieldstr ,',');
  45. } Else {
  46. $ Fieldstr = '*';
  47. }
  48. Self: $ SQL = "select {$ fieldstr} from {$ table} {$ where }";
  49. $ Result = mysql_query (self: $ SQL, $ this-> conn );
  50. $ ResuleRow = array ();
  51. $ I = 0;
  52. While ($ row = mysql_fetch_assoc ($ result )){
  53. Foreach ($ row as $ k => $ v ){
  54. $ ResuleRow [$ I] [$ k] = $ v;
  55. }
  56. $ I ++;
  57. }
  58. Return $ resuleRow;
  59. }
  60. /**
  61. * Add a record
  62. */
  63. Public function insert ($ table, $ data ){
  64. $ Values = '';
  65. $ Datas = '';
  66. Foreach ($ data as $ k => $ v ){
  67. $ Values. = $ k .',';
  68. $ Datas. = "'$ V '".',';
  69. }
  70. $ Values = rtrim ($ values ,',');
  71. $ Datas = rtrim ($ datas ,',');
  72. Self: $ SQL = "INSERT INTO {$ table} ({$ values}) VALUES ({$ datas })";
  73. If (mysql_query (self: $ SQL )){
  74. Return mysql_insert_id ();
  75. } Else {
  76. Return false;
  77. };
  78. }
  79. /**
  80. * Modify a record
  81. */
  82. Public function update ($ table, $ data, $ condition = array ()){
  83. $ Where = '';
  84. If (! Empty ($ condition )){
  85. Foreach ($ condition as $ k => $ v ){
  86. $ Where. = $ k. "= '". $ v. "' and ";
  87. }
  88. $ Where = 'where'. $ where. '1 = 1 ';
  89. }
  90. $ Updatastr = '';
  91. If (! Empty ($ data )){
  92. Foreach ($ data as $ k => $ v ){
  93. $ Updatastr. = $ k. "= '". $ v ."',";
  94. }
  95. $ Updatastr = 'set'. rtrim ($ updatastr ,',');
  96. }
  97. Self: $ SQL = "update {$ table} {$ updatastr} {$ where }";
  98. Return mysql_query (self: $ SQL );
  99. }
  100. /**
  101. * Delete a record
  102. */
  103. Public function delete ($ table, $ condition ){
  104. $ Where = '';
  105. If (! Empty ($ condition )){
  106. Foreach ($ condition as $ k => $ v ){
  107. $ Where. = $ k. "= '". $ v. "' and ";
  108. }
  109. $ Where = 'where'. $ where. '1 = 1 ';
  110. }
  111. Self: $ SQL = "delete from {$ table} {$ where }";
  112. Return mysql_query (self: $ SQL );
  113. }
  114. Public static function getLastSql (){
  115. Echo self: $ SQL;
  116. }
  117. }

  1. $ Db = array (
  2. 'Host' => 'localhost ',
  3. 'User' => 'root ',
  4. 'Password' => '',
  5. 'Database' => 'test ',
  6. )
  7. ?>

  1. Class db {
  2. Public $ conn;
  3. Public static $ SQL;
  4. Public static $ instance = null;
  5. Private function _ construct (){
  6. Require_once ('Db. config. php ');
  7. $ This-> conn = mysql_connect ($ db ['host'], $ db ['user'], $ db ['password']);
  8. If (! Mysql_select_db ($ db ['database'], $ this-> conn )){
  9. Echo "failed ";
  10. };
  11. Mysql_query ('set names utf8', $ this-> conn );
  12. }
  13. Public static function getInstance (){
  14. If (is_null (self: $ instance )){
  15. Self: $ instance = new db;
  16. }
  17. Return self: $ instance;
  18. }
  19. /**
  20. * Query a database
  21. */
  22. Public function select ($ table, $ condition = array (), $ field = array ()){
  23. $ Where = '';
  24. If (! Empty ($ condition )){
  25. Foreach ($ condition as $ k => $ v ){
  26. $ Where. = $ k. "= '". $ v. "' and ";
  27. }
  28. $ Where = 'where'. $ where. '1 = 1 ';
  29. }
  30. $ Fieldstr = '';
  31. If (! Empty ($ field )){
  32. Foreach ($ field as $ k => $ v ){
  33. $ Fieldstr. = $ v .',';
  34. }
  35. $ Fieldstr = rtrim ($ fieldstr ,',');
  36. } Else {
  37. $ Fieldstr = '*';
  38. }
  39. Self: $ SQL = "select {$ fieldstr} from {$ table} {$ where }";
  40. $ Result = mysql_query (self: $ SQL, $ this-> conn );
  41. $ ResuleRow = array ();
  42. $ I = 0;
  43. While ($ row = mysql_fetch_assoc ($ result )){
  44. Foreach ($ row as $ k => $ v ){
  45. $ ResuleRow [$ I] [$ k] = $ v;
  46. }
  47. $ I ++;
  48. }
  49. Return $ resuleRow;
  50. }
  51. /**
  52. * Add a record
  53. */
  54. Public function insert ($ table, $ data ){
  55. $ Values = '';
  56. $ Datas = '';
  57. Foreach ($ data as $ k => $ v ){
  58. $ Values. = $ k .',';
  59. $ Datas. = "'$ V '".',';
  60. }
  61. $ Values = rtrim ($ values ,',');
  62. $ Datas = rtrim ($ datas ,',');
  63. Self: $ SQL = "INSERT INTO {$ table} ({$ values}) VALUES ({$ datas })";
  64. If (mysql_query (self: $ SQL )){
  65. Return mysql_insert_id ();
  66. } Else {
  67. Return false;
  68. };
  69. }
  70. /**
  71. * Modify a record
  72. */
  73. Public function update ($ table, $ data, $ condition = array ()){
  74. $ Where = '';
  75. If (! Empty ($ condition )){
  76. Foreach ($ condition as $ k => $ v ){
  77. $ Where. = $ k. "= '". $ v. "' and ";
  78. }
  79. $ Where = 'where'. $ where. '1 = 1 ';
  80. }
  81. $ Updatastr = '';
  82. If (! Empty ($ data )){
  83. Foreach ($ data as $ k => $ v ){
  84. $ Updatastr. = $ k. "= '". $ v ."',";
  85. }
  86. $ Updatastr = 'set'. rtrim ($ updatastr ,',');
  87. }
  88. Self: $ SQL = "update {$ table} {$ updatastr} {$ where }";
  89. Return mysql_query (self: $ SQL );
  90. }
  91. /**
  92. * Delete a record
  93. */
  94. Public function delete ($ table, $ condition ){
  95. $ Where = '';
  96. If (! Empty ($ condition )){
  97. Foreach ($ condition as $ k => $ v ){
  98. $ Where. = $ k. "= '". $ v. "' and ";
  99. }
  100. $ Where = 'where'. $ where. '1 = 1 ';
  101. }
  102. Self: $ SQL = "delete from {$ table} {$ where }";
  103. Return mysql_query (self: $ SQL );
  104. }
  105. Public static function getLastSql (){
  106. Echo self: $ SQL;
  107. }
  108. }
  109. $ Db = db: getInstance ();
  110. // $ List = $ db-> select ('demo', array ('name' => 'Tom ', 'password' => 'Ds '), array ('name', 'password '));
  111. // Echo $ db-> insert ('demo', array ('name' => 'Recent use', 'password' => '123 '));
  112. // Echo $ db-> update ('demo', array ("name" => 'XXX', "password" => '123 '), array ('id' => 1 ));
  113. Echo $ db-> delete ('demo', array ('id' => '2 '));
  114. Db: getLastSql ();
  115. Echo"
    ";
  116. ?>

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.