Mysql database operation class-PHP Tutorial supporting php4 and php5

Source: Internet
Author: User
Tags mysql connect pconnect
Supports mysql database operations of php4 and php5. 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. I used to publish these library classes in PHPCHIAN. The address is the front-end of htt which has been using PHP5. it is indeed very nice to use. now, in order to be able to run on the machine's virtual host, you 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 that many of my reposted articles did not describe the source, and I deleted all my copyrights .)

Yesterday I changed the database operation class, which can be used just as I simplified the zend Framework.

The code is as follows:


/**
* 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 = "")
{
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 differerernt 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 ")
{
$ 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 ++;
}
} Else {
While ($ temp = @ mysql_fetch_object ($ this-> query_id )){
$ This-> record [$ I] = $ temp;
$ I ++;
}
}
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 ''. $ strsql .'';
$ 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 );
}
}
?>

Bytes. I used to publish these library classes in PHPCHIAN. The address is htt...

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.