Php for mysql code generation Assistant (automatically generates class files based on Mysql fields)

Source: Internet
Author: User
Tags mysql code

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:

Copy codeThe Code is as follows: <? Php
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 <mysql_num_fields ($ rel); $ 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 = '//////////////////////////////////// <br/> ';
$ Html = $ html. '// usage <br/> ';
$ Html = $ html. '// import <br/> ';
$ Html = $ html. '// ez_ SQL: http://jvmultimedia.com/docs/ezsql/ez_ SQL _help.htm ';
$ Html = $ html. '// require_once \'./db/ez_ SQL _core.php; <br/> ';
$ Html = $ html. '// require_once \'./db/ez_ SQL _mysql.php; <br/> ';
$ Html = $ html. '// $ db = new ezSQL_mysql ($ pai_db_user, $ pai_db_pass, $ pai_db_name, $ pai_db_host); <br/> ';
$ Html = $ html. '// $ db-> query (\ 'set names utf8 \'); <br/> ';
$ Html = $ html. '// call <br/> ';
$ Html = $ html. '// $ forum = new Forum ($ db); <br/> ';
$ Html = $ html. '// $ forum-> save ($ Posts); <br/> ';
$ Html = $ html. '// <br/> <br/> ';
$ Html = $ html. '// Author: <br/> ';
$ Html = $ html. '// remarks: <br/> ';
$ Html = $ html. '// Creation Time:'. date ('Y-m-d H: I: s'). '<br/> ';
$ Html = $ html. '<PRE> class'. $ tablename. '{<br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. 'Private $ db ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '// instantiate <br/> ';
$ Html = $ html. 'Public function'. $ tablename. '($ db) {<br/> ';
$ Html = $ html. '$ this-> db = $ db; <br/> ';
$ Html = $ html. '} <br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '// save record <br/> ';
$ Html = $ html. 'public function save ($ arry) {<br/> ';
For ($ I = 0; $ I <count ($ field); $ I ++ ){
$ Key = $ key. $ field [$ I]. ',';
$ Val = $ val. '\' {$ arry ['. $ field [$ I].']} \ ',';
}
$ SQL = "\" insert ". $ tablename. "(". rtrim ($ key ,","). ") values (". rtrim ($ val ,","). ")\"";
$ Html = $ html. '$ SQL ='. $ SQL. '; <br/> ';
$ Html = $ html. 'Return $ this-> db-> query ($ SQL); <br/> ';
$ Html = $ html. '} <br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '// update record based on primary key <br/> ';
$ Html = $ html. 'public function update ($ arry) {<br/> ';
For ($ I = 0; $ I <count ($ field); $ 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. '; <br/> ';
$ Html = $ html. 'Return $ this-> db-> query ($ SQL); <br/> ';
$ Html = $ html. '} <br/> ';
// PostID = '{$ Posts ['postid']}' ";
$ Html = $ html. '<br/> ';
$ Html = $ html. '// delete a record based on the primary key <br/> ';
$ Html = $ html. 'public function delete ($ '. $ this-> primary_key.') {<br/> ';
$ SQL = "\" delete from ". $ tablename." where ". $ this-> primary_key." = \ ". $". $ this-> primary_key;
$ Html = $ html. '$ SQL ='. $ SQL. '; <br/> ';
$ Html = $ html. 'Return $ this-> db-> query ($ SQL); <br/> ';
$ Html = $ html. '} <br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '// query a record based on the primary key <br/> ';
$ Html = $ html. 'public function getQueryById ($ '. $ this-> primary_key.') {<br/> ';
$ SQL = "\" select * from ". $ tablename." where ". $ this-> primary_key." = \ ". $". $ this-> primary_key;
$ Html = $ html. '$ SQL ='. $ SQL. '; <br/> ';
$ Html = $ html. 'Return $ this-> db-> get_row ($ SQL); <br/> ';
$ Html = $ html. '} <br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '// query all records <br/> ';
$ Html = $ html. 'public function getQuery () {<br/> ';
$ SQL = "\" select * from ". $ tablename ."\"";
$ Html = $ html. '$ SQL ='. $ SQL. '; <br/> ';
$ Html = $ html. 'Return $ this-> db-> get_results ($ SQL); <br/> ';
$ Html = $ html. '} <br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '<br/> ';
$ Html = $ html. '} </PRE> ';
Echo $ html;
}
}
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> PHP For MySQL Helper v1.0 </title>
<Style>
Body {font-size: 14px ;}
H3 {padding: 0px; margin: 0px; background-color: #333; color: # FFF; font-size: 14px ;}
. Dblist,. tablelist,. show {width: 100%; padding: 10px 0 ;}
A: link {}
A: hover {color: # F00}
</Style>
</Head>
<Body>
<? Php
Ini_set ('default _ charset', 'utf-8 ');
Echo '<div class = \ 'dblist \'> ';
Echo '//-----------------------------------
$ DB = new db ();
$ Dblist = $ DB-> db_list ();
For ($ row = 0; $ row <count ($ dblist); $ row ++ ){
Echo '<a href = "? Databasename = '. $ dblist [$ row].' "> '. $ dblist [$ row].' </a> <br/> ';
}
//-----------------------------------
Echo '</div> ';
Echo '<div class = \ 'tablelist \'> ';
Echo '//-----------------------------------
If (isset ($ _ GET ['databasename']) {
$ Databasename = $ _ GET ['databasename'];
$ DB = new db ();
$ Tablelist = $ DB-> table_list ($ databasename );
For ($ I = 0; $ I <count ($ tablelist); $ I ++ ){
Echo '<a href = "db. php? Databasename = '. $ databasename. '& tablename = '. $ tablelist [$ I]. '"> '. $ tablelist [$ I]. '</a> <br/> ';
}
}
//-----------------------------------
Echo '</div> ';
Echo '<div class = \ 'Show \'> ';
Echo '//-----------------------------------
If (isset ($ _ GET ['databasename']) & isset ($ _ GET ['tablename']) {
$ Databasename = $ _ GET ['databasename'];
$ Tablename = $ _ GET ['tablename'];
$ DB = new db ();
$ DB-> showclass ($ databasename, $ tablename );
}
//-----------------------------------
Echo '</div> ';
?>
</Body>
</Html>

Package and download code

Related Article

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.