The implementation code of the class file is automatically generated based on the fields in Mysql. For more information about php, see. Automatically Generate a class file based on the fields in Mysql:
But you need to import:
Require_once./db/ez_ SQL _core.php;
Require_once./db/ez_ SQL _mysql.php;
Help document: http://jvmultimedia.com/docs/ezsql/ez_ SQL _help.htm
:
Core code:
The code is as follows:
Class db {
/*************************************** *******************************
* Author: fangjun (fangjunai@163.com)
* Name...: PHP_For_MySQL_Helper v1.0
* Desc...: automatically generates database operation classes.
* Date ..: 2011-7-22
/*************************************** *******************************/
Private $ db_server = 'localhost ';
Private $ db_username = 'root ';
Private $ db_password = '';
Private $ primary_key = null;
// Initialization
Public function _ construct (){
$ This-> db_conn = mysql_connect ($ this-> db_server, $ this-> db_username, $ this-> db_password) or die ('Error: '. mysql_error ());
}
// Return the database name
Public function db_list (){
$ Databasename = array ();
$ I = 0;
$ List = mysql_list_dbs ($ this-> db_conn );
While ($ row = mysql_fetch_object ($ list )){
$ Databasename [$ I] = $ row-> Database;
$ I ++;
}
Mysql_close ();
Return $ databasename;
}
// Return the database name
Public function table_list ($ databasename ){
$ Tablename = array ();
$ I = 0;
$ Result = @ mysql_list_tables ($ databasename );
While ($ row = mysql_fetch_array ($ result, MYSQL_NUM )){
$ Tablename [$ I] = $ row [0];
$ I ++;
}
Mysql_free_result ($ result );
Mysql_close ();
Return $ tablename;
}
// Return fields in the table
Public function field_list ($ databasename, $ tablename ){
$ Fieldname = array ();
$ V = 0;
Mysql_select_db ($ databasename, $ this-> db_conn );
$ Rel = mysql_query ("select * from". $ tablename );
For ($ I = 0; $ I $ Meta = mysql_fetch_field ($ rel );
If ($ meta ){
If ($ meta-> primary_key = 1 ){
$ This-> primary_key = $ meta-> name;
} Else {
$ Fieldname [$ v] = $ meta-> name;
$ V ++;
}
}
}
Mysql_close ();
Return $ fieldname;
}
Public function showclass ($ databasename, $ tablename ){
$ Field = $ this-> field_list ($ databasename, $ tablename );
$ This-> primary_key;
$ Key = null;
$ Val = null;
$ SQL = null;
$ Tmp = null;
$ Html = '////////////////////////////////////
';
$ Html = $ html. '// Usage
';
$ Html = $ html. '// Import
';
$ Html = $ html. '// ez_ SQL: http://jvmultimedia.com/docs/ezsql/ez_ SQL _help.htm ';
$ Html = $ html. '// require_once \'./db/ez_ SQL _core.php;
';
$ Html = $ html. '// require_once \'./db/ez_ SQL _mysql.php;
';
$ Html = $ html. '// $ db = new ezSQL_mysql ($ pai_db_user, $ pai_db_pass, $ pai_db_name, $ pai_db_host );
';
$ Html = $ html. '// $ db-> query (\ 'set names utf8 \');
';
$ Html = $ html. '// call
';
$ Html = $ html. '// $ forum = new Forum ($ db );
';
$ Html = $ html. '// $ forum-> save ($ Posts );
';
$ Html = $ html. '////////////////////////////////////
';
$ Html = $ html. '// author:
';
$ Html = $ html. '// note:
';
$ Html = $ html. '// creation time:'. date ('Y-m-d H: I: s ').'
';
$ Html = $ html .'
Class '. $ tablename .'{
';
$ Html = $ html .'
';
$ Html = $ html. 'private $ db ';
$ Html = $ html .'
';
$ Html = $ html .'
';
$ Html = $ html. '// instantiate
';
$ Html = $ html. 'public function'. $ tablename. '($ db ){
';
$ Html = $ html. '$ this-> db = $ db;
';
$ Html = $ html .'}
';
$ Html = $ html .'
';
$ Html = $ html. '// save the record
';
$ Html = $ html. 'public function save ($ arry ){
';
For ($ I = 0; $ I
$ Key = $ key. $ field [$ I]. ',';
$ Val = $ val. '\' {$ arry ['. $ field [$ I].']} \ ',';
}
$ SQL = "\" insert ". $ tablename. "(". rtrim ($ key ,","). ") values (". rtrim ($ val ,","). ")\"";
$ Html = $ html. '$ SQL ='. $ SQL .';
';
$ Html = $ html. 'return $ this-> db-> query ($ SQL );
';
$ Html = $ html .'}
';
$ Html = $ html .'
';
$ Html = $ html. '// update a record based on the primary key
';
$ Html = $ html. 'public function update ($ arry ){
';
For ($ I = 0; $ I
$ Tmp = $ tmp. $ field [$ I]. '=\' {$ arry ['. $ field [$ I]. ']} \', ';
}
$ SQL = "\" update ". $ tablename. "set ". rtrim ($ tmp ,","). "where ". $ this-> primary_key. '= ". $ arry ['. $ this-> primary_key. ']';
$ Html = $ html. '$ SQL ='. $ SQL .';
';
$ Html = $ html. 'return $ this-> db-> query ($ SQL );
';
$ Html = $ html .'}
';
// PostID = '{$ Posts ['postid']}' ";
$ Html = $ html .'
';
$ Html = $ html. '// delete a record based on the primary key
';
$ Html = $ html. 'public function delete ($ '. $ this-> primary_key .'){
';
$ SQL = "\" delete from ". $ tablename." where ". $ this-> primary_key." = \ ". $". $ this-> primary_key;
$ Html = $ html. '$ SQL ='. $ SQL .';
';
$ Html = $ html. 'return $ this-> db-> query ($ SQL );
';
$ Html = $ html .'}
';
$ Html = $ html .'
';
$ Html = $ html. '// query a record based on the primary key
';
$ Html = $ html. 'public function getQueryById ($ '. $ this-> primary_key .'){
';
$ SQL = "\" select * from ". $ tablename." where ". $ this-> primary_key." = \ ". $". $ this-> primary_key;
$ Html = $ html. '$ SQL ='. $ SQL .';
';
$ Html = $ html. 'return $ this-> db-> get_row ($ SQL );
';
$ Html = $ html .'}
';
$ Html = $ html .'
';
$ Html = $ html. '// query all records
';
$ Html = $ html. 'public function getQuery (){
';
$ SQL = "\" select * from ". $ tablename ."\"";
$ Html = $ html. '$ SQL ='. $ SQL .';
';
$ Html = $ html. 'return $ this-> db-> get_results ($ SQL );
';
$ Html = $ html .'}
';
$ Html = $ html .'
';
$ Html = $ html .'
';
$ Html = $ html .'}
';
Echo $ html;
}
}
?>
PHP For MySQL Helper v1.0
Ini_set ('default _ charset', 'utf-8 ');
Echo'
';
Echo 'Select database ';
//-----------------------------------
$ DB = new db ();
$ Dblist = $ DB-> db_list ();
For ($ row = 0; $ row Echo ''. $ dblist [$ row].'
';
}
//-----------------------------------
Echo'
';
Echo'
';
Echo 'Select table ';
//-----------------------------------
If (isset ($ _ GET ['databasename']) {
$ Databasename = $ _ GET ['databasename'];
$ DB = new db ();
$ Tablelist = $ DB-> table_list ($ databasename );
For ($ I = 0; $ I Echo ''. $ tablelist [$ I].'
';
}
}
//-----------------------------------
Echo'
';
Echo'
';
Echo 'automatically generate data class ';
//-----------------------------------
If (isset ($ _ GET ['databasename']) & isset ($ _ GET ['tablename']) {
$ Databasename = $ _ GET ['databasename'];
$ Tablename = $ _ GET ['tablename'];
$ DB = new db ();
$ DB-> showclass ($ databasename, $ tablename );
}
//-----------------------------------
Echo'
';
?>
Package and download code