Phpmysql classic database connection class code _ PHP Tutorial

Source: Internet
Author: User
Phpmysql classic database connection code. Functionmysql tutorial connector (): class constructor, defining and containing configuration information functionconnectmysql (): open database tutorial connection functionclose (): Close database connection fu function mysql tutorial connector (): class constructor, defining and including configuration information
Function connectmysql (): Open the database tutorial connection
Function close (): Closes the database connection.
Function returnsql ($ SQL): executes a statement and returns an array of a row.
Function executesql ($ SQL): executes a query and returns whether the query is successful.
Function returndb ($ SQL): executes a query and returns a dataset.
Function selectlimit ($ SQL, $ offset_ B, $ offset_n = 0)
: Query by page and return dataset parameters (SQL statement, start position, number of read rows)
Function datearray ($ SQL, $ startid, $ endid)
: Paging query. two-dimensional array parameters are returned (SQL statement, start position, and number of read rows)
Function getarray ($ SQL): executes the query of two fields and returns an array in the format of array [row ["0"] => row ["1"]


*/

Class mysqlconnector
{
/* Public: database connection parameter */
Var $ dbhost; // server address
Var $ dbname; // database name
Var $ dbusername; // connection account
Var $ dbpassword; // connection password
Var $ setnames; // database encoding

Function mysqlconnector () // Constructor, database link configuration
{
$ This-> dbname = "xixia ";
$ This-> dbhost = "localhost ";
$ This-> dbusername = "root ";
$ This-> dbpassword = "123456 ";
$ This-> setnames = "gbk ";
}

Function connectmysql () // link to the database and return the active connection
{
$ Openconn = mysql_pconnect ($ this-> dbhost, $ this-> dbusername, $ this-> dbpassword) or die ("database connection error, please check the configuration! ");
Mysql_query ("set names '". $ this-> setnames. "'", $ openconn );
Mysql_select_db ($ this-> dbname, $ openconn );
Return $ openconn;
}

/*
*
* Execute the query statement and return the array of a row.
*/

Function returnsql ($ SQL)
{
$ Array_result = "";

// Mysql_unbuffered_query
$ Db_result = mysql_query ($ SQL, $ this-> connectmysql ());
If ($ db_result ){
$ Array_result = mysql_fetch_array ($ db_result );
}
Mysql_free_result ($ db_result); // release the record set
Return $ array_result;

}

/*
*
* Execute the query statement and return data.
*
*/

Function returndb ($ SQL)
{
$ Db_result = mysql_query ($ SQL, $ this-> connectmysql ());
Return $ db_result;

}

/*
*
* Execute the query statement and return an array of two columns, mainly used in the drop-down box. The first column is values, and the last column is option.
*
*/

Function getarray ($ SQL)
{
$ Array_result = array ();

$ Db_result = mysql_query ($ SQL, $ this-> connectmysql ());
If ($ db_result ){
While ($ row = mysql_fetch_row ($ db_result ))
{
$ Array_result [$ row [0] = $ row [1];
}
}

Return $ array_result;

}

/*
*
* Execute an SQL statement and return whether the execution is successful.
*
*/

Function executesql ($ SQL)
{
// $ SQL = str_replace ("", "\", $ SQL );
$ Result = mysql_query ($ SQL, $ this-> connectmysql ());
If (! $ Result ){
Echo" ";
Return false;
} Else {
Return true;
}
}

/*

Reads SQL statements by Page and returns the record set,
The parameters are SQL statements, the number of start lines, and the number of reads (when the 2 brother parameter is passed, the number of start lines is the number of read lines)
*/

Function selectlimit ($ SQL, $ offset_ B, $ offset_n = 0)
{

$ Result = "";
$ This-> checklink ($ SQL );
If (! $ Offset_n ){
$ Limit = "limit". $ offset_ B;
} Else {
$ Limit = "limit". $ offset_ B. ",". $ offset_n;
}
$ SQL. = $ limit;
// Echo"
";
// Echo $ SQL;

$ Result = $ this-> returndb ($ SQL );
Return $ result;
}

/*
*
* Convert a dataset into an array
*
*/
Function datearray ($ SQL, $ startid, $ endid)
{
$ Array_result = array ();
$ Db_result = $ this-> selectlimit ($ SQL, $ startid, $ endid); // read the dataset according to the SQL statement

If ($ db_result) {// The dataset exists
$ I = 0;
While ($ row = mysql_fetch_row ($ db_result) // cyclically fills the array
{
$ Array_result [$ I] = $ row;
$ I ++;
}
}

Return $ array_result;

}

/*
*
* Close the link
*
*/
Function close ()
{
If ($ this-> linkid! = Null)
{
Mysql_close ($ this-> linkid );
Unset ($ this );
}
}

}
/*

* Use case:
$ Conn = new mysqlconnector (); // instantiate
$ Db = & $ conn;

$ Db-> returnsql ($ SQL) // execute the query


*/

Http://www.bkjia.com/PHPjc/630772.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630772.htmlTechArticlefunction mysql tutorial connector (): class constructor, define and include configuration information function connectmysql (): open database tutorial connection function close (): close database connection fu...

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.