Database operation base classes written by yourself, including the most basic CURD operations, which can be integrated into the framework.
- // Configuration file
- $ Db = array (
- 'Host' => 'localhost ',
- 'User' => 'root ',
- 'Password' => '',
- 'Database' => 'test ',
- )
- ?>
- // Php class
- Class db {
- Public $ conn;
- Public static $ SQL;
- Public static $ instance = null;
- Private function _ construct (){
- Require_once ('Db. config. php ');
- $ This-> conn = mysql_connect ($ db ['host'], $ db ['user'], $ db ['password']);
- If (! Mysql_select_db ($ db ['database'], $ this-> conn )){
- Echo "failed ";
- };
- Mysql_query ('set names utf8', $ this-> conn );
- }
- Public static function getInstance (){
- If (is_null (self: $ instance )){
- Self: $ instance = new db;
- }
- Return self: $ instance;
- }
- /**
- * Query a database
- */
- Public function select ($ table, $ condition = array (), $ field = array ()){
- $ Where = '';
- If (! Empty ($ condition )){
- Foreach ($ condition as $ k => $ v ){
- $ Where. = $ k. "= '". $ v. "' and ";
- }
- $ Where = 'where'. $ where. '1 = 1 ';
- }
- $ Fieldstr = '';
- If (! Empty ($ field )){
- Foreach ($ field as $ k => $ v ){
- $ Fieldstr. = $ v .',';
- }
- $ Fieldstr = rtrim ($ fieldstr ,',');
- } Else {
- $ Fieldstr = '*';
- }
- Self: $ SQL = "select {$ fieldstr} from {$ table} {$ where }";
- $ Result = mysql_query (self: $ SQL, $ this-> conn );
- $ ResuleRow = array ();
- $ I = 0;
- While ($ row = mysql_fetch_assoc ($ result )){
- Foreach ($ row as $ k => $ v ){
- $ ResuleRow [$ I] [$ k] = $ v;
- }
- $ I ++;
- }
- Return $ resuleRow;
- }
- /**
- * Add a record
- */
- Public function insert ($ table, $ data ){
- $ Values = '';
- $ Datas = '';
- Foreach ($ data as $ k => $ v ){
- $ Values. = $ k .',';
- $ Datas. = "'$ V '".',';
- }
- $ Values = rtrim ($ values ,',');
- $ Datas = rtrim ($ datas ,',');
- Self: $ SQL = "INSERT INTO {$ table} ({$ values}) VALUES ({$ datas })";
- If (mysql_query (self: $ SQL )){
- Return mysql_insert_id ();
- } Else {
- Return false;
- };
- }
- /**
- * Modify a record
- */
- Public function update ($ table, $ data, $ condition = array ()){
- $ Where = '';
- If (! Empty ($ condition )){
- Foreach ($ condition as $ k => $ v ){
- $ Where. = $ k. "= '". $ v. "' and ";
- }
- $ Where = 'where'. $ where. '1 = 1 ';
- }
- $ Updatastr = '';
- If (! Empty ($ data )){
- Foreach ($ data as $ k => $ v ){
- $ Updatastr. = $ k. "= '". $ v ."',";
- }
- $ Updatastr = 'set'. rtrim ($ updatastr ,',');
- }
- Self: $ SQL = "update {$ table} {$ updatastr} {$ where }";
- Return mysql_query (self: $ SQL );
- }
- /**
- * Delete a record
- */
- Public function delete ($ table, $ condition ){
- $ Where = '';
- If (! Empty ($ condition )){
- Foreach ($ condition as $ k => $ v ){
- $ Where. = $ k. "= '". $ v. "' and ";
- }
- $ Where = 'where'. $ where. '1 = 1 ';
- }
- Self: $ SQL = "delete from {$ table} {$ where }";
- Return mysql_query (self: $ SQL );
- }
- Public static function getLastSql (){
- Echo self: $ SQL;
- }
- }
- $ Db = array (
- 'Host' => 'localhost ',
- 'User' => 'root ',
- 'Password' => '',
- 'Database' => 'test ',
- )
- ?>
- Class db {
- Public $ conn;
- Public static $ SQL;
- Public static $ instance = null;
- Private function _ construct (){
- Require_once ('Db. config. php ');
- $ This-> conn = mysql_connect ($ db ['host'], $ db ['user'], $ db ['password']);
- If (! Mysql_select_db ($ db ['database'], $ this-> conn )){
- Echo "failed ";
- };
- Mysql_query ('set names utf8', $ this-> conn );
- }
- Public static function getInstance (){
- If (is_null (self: $ instance )){
- Self: $ instance = new db;
- }
- Return self: $ instance;
- }
- /**
- * Query a database
- */
- Public function select ($ table, $ condition = array (), $ field = array ()){
- $ Where = '';
- If (! Empty ($ condition )){
- Foreach ($ condition as $ k => $ v ){
- $ Where. = $ k. "= '". $ v. "' and ";
- }
- $ Where = 'where'. $ where. '1 = 1 ';
- }
- $ Fieldstr = '';
- If (! Empty ($ field )){
- Foreach ($ field as $ k => $ v ){
- $ Fieldstr. = $ v .',';
- }
- $ Fieldstr = rtrim ($ fieldstr ,',');
- } Else {
- $ Fieldstr = '*';
- }
- Self: $ SQL = "select {$ fieldstr} from {$ table} {$ where }";
- $ Result = mysql_query (self: $ SQL, $ this-> conn );
- $ ResuleRow = array ();
- $ I = 0;
- While ($ row = mysql_fetch_assoc ($ result )){
- Foreach ($ row as $ k => $ v ){
- $ ResuleRow [$ I] [$ k] = $ v;
- }
- $ I ++;
- }
- Return $ resuleRow;
- }
- /**
- * Add a record
- */
- Public function insert ($ table, $ data ){
- $ Values = '';
- $ Datas = '';
- Foreach ($ data as $ k => $ v ){
- $ Values. = $ k .',';
- $ Datas. = "'$ V '".',';
- }
- $ Values = rtrim ($ values ,',');
- $ Datas = rtrim ($ datas ,',');
- Self: $ SQL = "INSERT INTO {$ table} ({$ values}) VALUES ({$ datas })";
- If (mysql_query (self: $ SQL )){
- Return mysql_insert_id ();
- } Else {
- Return false;
- };
- }
- /**
- * Modify a record
- */
- Public function update ($ table, $ data, $ condition = array ()){
- $ Where = '';
- If (! Empty ($ condition )){
- Foreach ($ condition as $ k => $ v ){
- $ Where. = $ k. "= '". $ v. "' and ";
- }
- $ Where = 'where'. $ where. '1 = 1 ';
- }
- $ Updatastr = '';
- If (! Empty ($ data )){
- Foreach ($ data as $ k => $ v ){
- $ Updatastr. = $ k. "= '". $ v ."',";
- }
- $ Updatastr = 'set'. rtrim ($ updatastr ,',');
- }
- Self: $ SQL = "update {$ table} {$ updatastr} {$ where }";
- Return mysql_query (self: $ SQL );
- }
- /**
- * Delete a record
- */
- Public function delete ($ table, $ condition ){
- $ Where = '';
- If (! Empty ($ condition )){
- Foreach ($ condition as $ k => $ v ){
- $ Where. = $ k. "= '". $ v. "' and ";
- }
- $ Where = 'where'. $ where. '1 = 1 ';
- }
- Self: $ SQL = "delete from {$ table} {$ where }";
- Return mysql_query (self: $ SQL );
- }
- Public static function getLastSql (){
- Echo self: $ SQL;
- }
- }
- $ Db = db: getInstance ();
- // $ List = $ db-> select ('demo', array ('name' => 'Tom ', 'password' => 'Ds '), array ('name', 'password '));
- // Echo $ db-> insert ('demo', array ('name' => 'Recent use', 'password' => '123 '));
- // Echo $ db-> update ('demo', array ("name" => 'XXX', "password" => '123 '), array ('id' => 1 ));
- Echo $ db-> delete ('demo', array ('id' => '2 '));
- Db: getLastSql ();
- Echo"
"; - ?>
|