Marc by Marc Jacobs website PHP for MySQL code generation helper automatically generates class files based on fields in MySQL

Source: Internet
Author: User
Tags mysql code rtrim
Automatically generate class files 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 the Code code as follows:


Class db{
/**********************************************************************
* Author:fangjun (fangjunai@163.com)
* Name: Php_for_mysql_helper v1.0
* DESC: Auto-Generate Database Operation class
* 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 ());
}
Returns the database name so
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;
}
Returns the database name so
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 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. ' How to use
';
$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 ($cfg _db_user, $cfg _db_pass, $cfg _db_name, $cfg _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. '//instantiation
';
$html = $html. ' Public function '. $tablename. ' ($DB) {
';
$html = $html. ' $this->db = $db;
';
$html = $html. '}
';
$html = $html. '
';
$html = $html. '//Save Record
';
$html = $html. ' Public Function Save ($arry) {
';
for ($i =0; $i $key = $key. $field [$i]. ', ';
$val = $val. ' \ ' {$arry ['. $field [$i]. '} \',';
}
$sql = "\" INSERT into ". $tablename." (". RTrim ($key,", ").") VALUES (". RTrim ($val,", ").") \"";
$html = $html. ' $sql = '. $sql. ';
';
$html = $html. ' Return $this->db->query ($sql);
';
$html = $html. '}
';
$html = $html. '
';
$html = $html. '//Update records based on 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 records based on 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 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;
}
}
?>




<title>PHP for MySQL Helper v1.0</title>



Ini_set (' Default_charset ', ' utf-8 ');
Echo ';
Echo '

Please select a database

';
//-----------------------------------
$DB = new DB ();
$dblist = $DB->db_list ();
for ($row =0; $row Echo '. $dblist [$row]. '
';
}
//-----------------------------------
Echo ';
Echo ';
Echo '

Please select a 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 generates data classes ';
//-----------------------------------
if (isset ($_get[' databasename ')) && isset ($_get[' tablename ') ) {
$databasename = $_get[' databasename ');
$tablename = $_get[' tablename ');
$DB = new DB ();
$DB->showclass ($databasename, $tablename);
}
//-----------------------------------
Echo ';
?>



Code package download

The above describes the Marc by Marc Jacobs website PHP for MySQL code generation assistant according to MySQL Automatically generated class files, including the Marc by Marc Jacobs website, hope to be interested in the PHP tutorial friends helpful.

  • 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.