PHPFORMYSQL code generation Assistant (automatically generates class files based on Mysql Fields

Source: Internet
Author: User
The implementation code of the class file is automatically generated based on the fields in Mysql. For more information about php, see.

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:

:


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'

';
?>

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.