The front-end has been using PhP5, which is indeed very nice to use. Now, in order to be able to run on the machine's virtual host, I have to change it to PhP4. These library classes I used to send in phpchian, the address is http://www.phpchina.com/bbs/viewthread.php? Tid = 5687 & Highlight =. (I searched the internet a few days ago and found many of my articles reposted. Article I have not explained the source and deleted all my copyrights .)
Yesterday I changed the database operation class, which can be used just as I simplified the Zend framework.
CodeAs follows:
<? PHP
/**
* Filename: db_mysql.class.php
* @ Package: phpbean
* @ Author: feifengxlq <[email] feifengxlq@gmail.com [/Email]>
* @ Copyright: Copyright 2006 feifengxlq
* @ License: version 1.2
* Create: 2006-5-30
* Modify: 2006-10-19 by feifengxlq
* Description: The interface of MySQL.
*
* Example:
* // Select action (first mode) //////////////////////////////
$ Mysql = new db_mysql ("localhost", "root ");
$ Rs = $ mysql-> query ("select * from test ");
For ($ I = 0; $ I <$ mysql-> num_rows ($ RS); $ I ++)
$ Record [$ I] = $ mysql-> seek ($ I );
Print_r ($ record );
$ Mysql-> close ();
* /// // Select action (second mode) //////////////////////////////
$ Mysql = new db_mysql ("localhost", "root ");
$ Rs = $ mysql-> execute ("select * from test ");
Print_r ($ RS );
$ Mysql-> close ();
* // Insert action //////////////////////// ////
$ Mysql = new db_mysql ("localhost", "root ");
$ Mysql-> query ("insert into test (username) values ('test from my db_mysql ')");
Printf ("% s", $ mysql-> insert_id ());
$ Mysql-> close ();
*/
Class MySQL {
/* Private: Connection parameters */
VaR $ host = "localhost ";
VaR $ database = "";
VaR $ user = "root ";
VaR $ Password = "";
/* Private: configuration parameters */
VaR $ pconnect = false;
VaR $ DEBUG = false;
/* Private: Result array and current row number */
VaR $ link_id = 0;
VaR $ query_id = 0;
VaR $ record = array ();
/**
* Construct
*
* @ Param string $ host
* @ Param string $ user
* @ Param string $ Password
* @ Param string $ Database
*/
Function _ construct ($ host = "localhost", $ user = "root", $ Password = "", $ database = "")
{
$ This-> set ("host", $ host );
$ This-> set ("user", $ user );
$ This-> set ("password", $ password );
$ This-> set ("Database", $ database );
$ This-> connect ();
}
/**
* Set the value for the param of this class
*
* @ Param string $ VaR
* @ Param string $ Value
*/
Function set ($ var, $ value)
{
$ This-> $ Var = $ value;
}
/**
* Connect to a MySQL server, and choose the database.
*
* @ Param string $ Database
* @ Param string $ host
* @ Param string $ user
* @ Param string $ Password
* @ Return link_id
*/
Function connect ($ database = "", $ host = "", $ user = "", $ Password = "")
{
If (! Empty ($ database) $ this-> set ("Database", $ database );
If (! Empty ($ host) $ this-> set ("host", $ host );
If (! Empty ($ user) $ this-> set ("user", $ user );
If (! Empty ($ password) $ this-> set ("password", $ password );
If ($ this-> link_id = 0)
{
If ($ this-> pconnect)
$ This-> link_id = @ mysql_pconnect ($ this-> host, $ this-> User, $ this-> password );
Else
$ This-> link_id = @ mysql_connect ($ this-> host, $ this-> User, $ this-> password );
If (! $ This-> link_id)
Die ("MySQL connect error in". _ function _. "():". mysql_errno (). ":". mysql_error ());
If (! @ Mysql_select_db ($ this-> database, $ this-> link_id ))
Die ("MySQL select database error in". _ function _. "():". mysql_errno (). ":". mysql_error ());
}
Return $ this-> link_id;
}
/**
* query a SQL into the database
* @ Param string $ strsql
* @ return query_id
* /
function query ($ strsql = "")
{< br> If (empty ($ strsql) Die ("MySQL error :". _ function __. "() strsql is empty! ");
if ($ this-> link_id = 0) $ this-> connect ();
if ($ this-> Debug) printf ("Debug query SQL: % s", $ strsql);
$ this-> query_id = @ mysql_query ($ strsql, $ this-> link_id );
If (! $ This-> query_id) Die ("MySQL query fail, invalid SQL :". $ strsql. ". ");
return $ this-> query_id;
}
/**
* query a SQL into the database, while it is differernt from the query () method,
* This method will return a record (array );
* @ Param string $ strsql
* @ Param string $ style
* @ return $ record is a array ()
*/
function execute ($ strsql, $ style = "array")
{< br> $ this-> query ($ strsql );
If (! Empty ($ this-> record) $ this-> record = array ();
$ I = 0;
if ($ style = "array ") {
while ($ temp = @ mysql_fetch_array ($ this-> query_id) {
$ this-> record [$ I] = $ temp;
$ I ++;
}< BR >}else {
while ($ temp = @ mysql_fetch_object ($ this-> query_id )) {
$ this-> record [$ I] = $ temp;
$ I ++;
}< BR >}< br> unset ($ I);
unset ($ temp);
return $ this-> record;
}
/**
* Seek, but not equal to mysql_data_seek. This methord will return a list.
*
* @ Param int $ POS
* @ Param string $ Style
* @ Return record
*/
Function seek ($ Pos = 0, $ style = "array ")
{
If (! @ Mysql_data_seek ($ this-> query_id, $ POS ))
Die ("error in". _ function _. "(): Can not seek to row". $ pos ."! ");
$ Result = @ ($ style = "array ")? Mysql_fetch_array ($ this-> query_id): mysql_fetch_object ($ this-> query_id );
If (! $ Result) Die ("error in". _ function _. "(): Can not fetch data! ");
Return $ result;
}
/**
* Free the result of query
*
*/
Function free ()
{
If ($ this-> query_id) & ($ this-> query_id! = 0) @ mysql_free_result ($ this-> query_id );
}
/**
* Evaluate the result (size, width)
*
* @ Return num
*/
Function affected_rows ()
{
Return @ mysql_affected_rows ($ this-> link_id );
}
Function num_rows ()
{
Return @ mysql_num_rows ($ this-> query_id );
}
Function num_fields ()
{
Return @ mysql_num_fields ($ this-> query_id );
}
Function insert_id ()
{
Return @ mysql_insert_id ($ this-> link_id );
}
Function close ()
{
$ This-> free ();
If ($ this-> link_id! = 0) @ mysql_close ($ this-> link_id );
If (mysql_errno ()! = 0) Die ("MySQL error:". mysql_errno (). ":". mysql_error ());
}
Function select ($ strsql, $ number, $ offset)
{
If (empty ($ number )){
Return $ this-> execute ($ strsql );
} Else {
Return $ this-> execute ($ strsql. 'limit'. $ offset. ','. $ number );
}
}
Function _ destruct ()
{
$ This-> close ();
$ This-> set ("user ","");
$ This-> set ("host ","");
$ This-> set ("password ","");
$ This-> set ("Database ","");
}
}
?>
On this basis, I encapsulate four basic operations, namely sidu (select, insert, update, and delete), as the module of the simplified Zend framework. The Code is as follows (this is a lazy write without writing comments ..) :
<?
Class module {
VaR $ mysql;
VaR $ tbname;
VaR $ DEBUG = false;
Function _ construct ($ tbname ){
If (! Is_string ($ tbname) Die ('module need a ARGs of tablename ');
$ This-> tbname = $ tbname;
$ This-> MySQL = phpbean: Registry ('db ');
}
Function _ setdebug ($ DEBUG = true ){
$ This-> DEBUG = $ debug;
}
Function add ($ row ){
If (! Is_array ($ row) Die ('module error: Row shocould be an array ');
$ Strsql = 'insert into '''. $ this-> tbname .''';
$ Keys = '';
$ Values = '';
Foreach ($ row as $ key => $ value ){
$ Keys. = '''. $ key .'',';
$ Values. = '\ ''. $ value .'\'';
}
$ Keys = rtrim ($ keys ,',');
$ Values = rtrim ($ values ,',');
$ Strsql. = '('. $ keys. ') values ('. $ values .')';
If ($ this-> Debug) echo '<HR>'. $ strsql. '<HR> ';
$ This-> mysql-> query ($ strsql );
Return $ this-> mysql-> insert_id ();
}
Function query ($ strsql ){
Return $ this-> mysql-> execute ($ strsql );
}
Function count ($ where = ''){
$ Strsql = 'select count (*) as num from ''. $ this-> tbname .''';
If (! Empty ($ where) $ strsql. = $ where;
$ Rs = $ this-> mysql-> execute ($ strsql );
Return $ Rs [0] ['num'];
}
Function select ($ where = ''){
$ Strsql = 'select * From ''. $ this-> tbname .''';
If (! Empty ($ where) $ strsql. = $ where;
Return $ this-> mysql-> execute ($ strsql );
}
Function Delete ($ where = ''){
If (empty ($ where) Die ('error: The delete method need a condition! ');
Return $ this-> mysql-> query ('delete from ''. $ this-> tbname. '''. $ where );
}
Function Update ($ set, $ where ){
If (empty ($ where) Die ('error: The update method need a condition! ');
If (! Is_array ($ set) Die ('error: set must be an array! ');
$ Strsql = 'update'. $ this-> tbname .''';
// Get a string of Set
$ Strsql. = 'set ';
Foreach ($ set as $ key => $ value ){
$ Strsql. = '''. $ key. ''= \''. $ value .'\',';
}
$ Strsql = rtrim ($ strsql ,',');
Return $ this-> mysql-> query ($ strsql. ''. $ where );
}
function detail ($ where) {
If (empty ($ where) Die ('error: Where shocould not empty! ');
$ rs = $ this-> mysql-> query ('select * From ''. $ this-> tbname. '''. $ where);
return $ this-> mysql-> seek (0);
}< BR >?>