Mysqlphp connection class _ PHP Tutorial for an instance

Source: Internet
Author: User
Tags pconnect
An instance mysqlphp connection class. This mysql database connection article under the mysqlphp connection class of an instance was developed from a cms and is complete. You can call it directly. An instance mysql tutorial php Tutorial connects to an instance mysql php connection class below this mysql database connection article, I made from a cms, very complete. You can call it directly.

One instance mysql tutorial php Tutorial connection class
The following mysql database connection document is complete from a cms. You can call it directly.
*/

Class dbmysq {

Var $ querynum = 0;
Var $ link;
Var $ histories;
Var $ time;
Var $ tablepre;

Function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ dbcharset, $ pconnect = 0, $ tablepre ='', $ time = 0 ){
$ This-> time = $ time;
$ This-> tablepre = $ tablepre;
If ($ pconnect ){
If (! $ This-> link = mysql_pconnect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> halt ('Can not connect to mysql server ');
}
} Else {
If (! $ This-> link = mysql_connect ($ dbhost, $ dbuser, $ dbpw, 1 )){
$ This-> halt ('Can not connect to mysql server ');
}
}

If ($ this-> version ()> '4. 1 '){
If ($ dbcharset ){
Mysql_query ("set character_set_connection =". $ dbcharset. ", character_set_results =". $ dbcharset. ", character_set_client = binary", $ this-> link );
}

If ($ this-> version ()> '5. 0.1 '){
Mysql_query ("set SQL _mode ='' ", $ this-> link );
}
}

If ($ dbname ){
Mysql_select_db ($ dbname, $ this-> link );
}
}

Function fetch_array ($ query, $ result_type = mysql_assoc ){
Return mysql_fetch_array ($ query, $ result_type );
}

Function result_first ($ SQL, & $ data ){
$ Query = $ this-> query ($ SQL );
$ Data = $ this-> result ($ query, 0 );
}

Function fetch_first ($ SQL, & $ arr ){
$ Query = $ this-> query ($ SQL );
$ Arr = $ this-> fetch_array ($ query );
}

Function fetch_all ($ SQL, & $ arr ){
$ Query = $ this-> query ($ SQL );
While ($ data = $ this-> fetch_array ($ query )){
$ Arr [] = $ data;
}
}

Function cache_gc (){
$ This-> query ("delete from {$ this-> tablepre} sqlcaches where expiry <$ this-> time ");
}

Function query ($ SQL, $ type = '', $ cachetime = false ){
$ Func = $ type = 'unbuffered' & @ function_exists ('MySQL _ unbuffered_query ')? 'MySQL _ unbuffered_query ': 'MySQL _ query ';
If (! ($ Query = $ func ($ SQL, $ this-> link) & $ type! = 'Silent '){
$ This-> halt ('MySQL query error', $ SQL );
}
$ This-> querynum ++;
$ This-> histories [] = $ SQL;
Return $ query;
}

Function affected_rows (){
Return mysql_affected_rows ($ this-> link );
}

Function error (){
Return ($ this-> link )? Mysql_error ($ this-> link): mysql_error ());
}

Function errno (){
Return intval ($ this-> link )? Mysql_errno ($ this-> link): mysql_errno ());
}

Function result ($ query, $ row ){
$ Query = @ mysql_result ($ query, $ row );
Return $ query;
}

Function num_rows ($ query ){
$ Query = mysql_num_rows ($ query );
Return $ query;
}

Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}

Function free_result ($ query ){
Return mysql_free_result ($ query );
}

Function insert_id (){
Return ($ id = mysql_insert_id ($ this-> link)> = 0? $ Id: $ this-> result ($ this-> query ("select last_insert_id ()"), 0 );
}

Function fetch_row ($ query ){
$ Query = mysql_fetch_row ($ query );
Return $ query;
}

Function fetch_fields ($ query ){
Return mysql_fetch_field ($ query );
}

Function version (){
Return mysql_get_server_info ($ this-> link );
}

Function close (){
Return mysql_close ($ this-> link );
}

Function halt ($ message = '', $ SQL = ''){
Exit ('
Tip: Database error
SQL statement: '. $ SQL .'
Keyword: '. mysql_error ());
}

}

?>

The mysql database connection article under the connector php connection class was developed from a cms and complete. You can call it directly. An instance mysql tutorial php Tutorial connection...

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.