PHP for MySQL code generation Assistant (automatically generates class files according to the fields in MySQL) _php tips

Source: Internet
Author: User
Tags mysql code rtrim

Automatically generate class files according to 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

Image above:


Core code:

Copy Code code as follows:

<?php
Class db{
/**********************************************************************
* Author:fangjun (fangjunai@163.com)
* Name: Php_for_mysql_helper v1.0
* DESC: Automatically generate database Operations class
* Date.: 2011-7-22
/**********************************************************************/
Private $db _server = ' localhost ';
Private $db _username = ' root ';
Private $db _password = ';
Private $primary _key = null;
Class
Public Function __construct () {
$this->db_conn = mysql_connect ($this->db_server, $this->db_username, $this->db_password) or Die (' Error: ' . Mysql_error ());
}
Returns the name of the database
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 name of the database
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 the 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. ' Use method <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 ($cfg _db_user, $cfg _db_pass, $cfg _db_name, $cfg _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/><br/> ';
$html = $html. ' Author:<br/> ';
$html = $html. ' Remark:<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. '//instantiation <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 into". $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 records according to 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 records according to 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 <br/> ' according to the primary key;
$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. '//Check 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 ">
<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%;p adding:10px 0;
A:link {}
a:hover {color: #F00}
</style>
<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>

Code package download

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.