<? Php
Define ("MYSQL_ SQL _GETDATA", 1 );
Define ("MYSQL_ SQL _EXECUTE", 2 );
Class mysql_db {
Var $ _ server; // address of the Database server
Var $ _ user; // database connection account
Var $ _ password; // database connection password
Var $ _ dbname; // Database Name
Var $ _ persistency = false; // whether to use persistent connections
Var $ _ isConnect = false; // whether a database connection has been established
Var $ _ charset = "utf8"; // database connection Character Set
VaR $ _ isdebug = false; // whether the debug mode is used
VaR $ _ SQL = array (); // an array of executed SQL statements
VaR $ _ db_connect_id; // ID of the database connection object
VaR $ _ result; // The value returned by the execution Query
VaR $ _ record;
VaR $ _ rowset;
VaR $ _ errno = 0;
VaR $ _ error = "connection error ";
VaR $ _ checkdb = false;
Function mysql_db ($ dbserver, $ dbuser, $ dbpassword, $ database, $ persistency = false, $ autoconnect = false, $ checkdb = false)
{
$ This-> _ server = $ dbserver;
$ This-> _ user = $ dbuser;
$ This-> _ password = $ dbpassword;
$ This-> _ dbname = $ database;
$ This-> _ persistency = $ persistency;
$ This-> _ autoConnect = $ autoConnect;
$ This-> _ checkDB = $ checkdb;
If ($ autoConnect ){
$ This-> connection ();
}
}
Function connection ($ newlink = false)
{
If (! $ Newlink ){
If ($ this-> _ isconnect & isset ($ this-> _ db_connect_id )){
@ Mysql_close ($ this-> _ db_connect_id );
}
}
$ This-> _ db_connect_id = ($ this-> persistency )? @ Mysql_pconnect ($ this-> _ server, $ this-> _ User, $ this-> _ password): @ mysql_connect ($ this-> _ server, $ this-> _ User, $ this-> _ password, $ newlink );
If ($ this-> _ db_connect_id)
{
If ($ this-> Version ()> '4. 1 ')
{
If ($ this-> _ charset! = "")
{
@ Mysql_query ("set names '". str_replace ('-', '', $ this-> _ charset)."' ", $ this-> _ db_connect_id );
}
}
If ($ this-> Version ()> '5. 0 ')
{
@ Mysql_query ("set SQL _mode ='' ", $ this-> _ db_connect_id );
}
// Check whether the specified database is connected successfully
If ($ this-> _ checkdb ){
$ Dbname = mysql_query ('select database () ', $ this-> _ db_connect_id );
$ Dbname = mysql_fetch_array ($ dbname, mysql_num );
$ Dbname = trim ($ dbname [0]);
} Else {
$ Dbname = '';
}
If ($ dbname = $ this-> _ dbname | $ dbname = ''){
If (! @ Mysql_select_db ($ this-> _ dbname, $ this-> _ db_connect_id ))
{
@ Mysql_close ($ this-> _ db_connect_id );
$ This-> _ halt ("cannot use database". $ this-> _ dbname );
}
} Else {
If ($ this-> _ checkDB &&! $ NewLink ){
$ This-> connection (true );
}
}
Return true;
}
Else
{
$ This-> _ halt ('connect failed. ', false );
}
}
Function setCharset ($ charset ){
// $ Charset = str_replace ('-', '', $ charset );
$ This-> _ charset = $ charset;
}
Function setDebug ($ isDebug = true ){
$ This-> _ isDebug = $ isDebug;
}
Function query ($ SQL, $ type = '')
{
Return $ this-> _ runsql ($ SQL, mysql_ SQL _getdata, $ type );
}
Function execute ($ SQL)
{
Return $ this-> _ runsql ($ SQL, mysql_ SQL _execute, "unbuffered ");
}
Function _ runsql ($ SQL, $ sqltype = mysql_ SQL _getdata, $ type = '')
{
If ($ type = "unbuffered "){
$ This-> _ result = @ mysql_unbuffered_query ($ SQL, $ this-> _ db_connect_id );
} Else {
$ This-> _ result = @ mysql_query ($ SQL, $ this-> _ db_connect_id );
}
// Save the executed SQL statement in Test Mode
If ($ this-> _ isDebug ){
$ This-> _ SQL [] = $ SQL;
}
If ($ this-> _ result)
{
Return $ sqlType = MYSQL_ SQL _GETDATA? $ This-> getNumRows (): $ this-> getAffectedRows ();
} Else {
$ This-> _ halt ("Invalid SQL:". $ SQL );
Return false;
}
}
Function next ($ result_type = MYSQL_ASSOC ){
$ This-> fetchRow ($ result_type );
Return is_array ($ this-> _ record );
}
Function f ($ name ){
If (is_array ($ this-> _ record )){
Return $ this-> _ record [$ name];
} Else {
Return false;
}
}
Function fetchRow ($ result_type = MYSQL_ASSOC)
{
If ($ this-> _ result)
{
$ This-> _ record = @ mysql_fetch_array ($ this-> _ result, $ result_type );
Return $ this-> _ record;
} Else {
Return false;
}
}
Function getAll ($ SQL, $ primaryKey = "", $ result_type = MYSQL_ASSOC)
{
If ($ this-> _ runSQL ($ SQL, MYSQL_ SQL _GETDATA)> = 0 ){
Return $ this-> fetchAll ($ primaryKey, $ result_type );
} Else {
Return false;
}
}
Function getOne ($ SQL, $ result_type = MYSQL_ASSOC)
{
If ($ this-> _ runSQL ($ SQL, MYSQL_ SQL _GETDATA)> 0 ){
$ Arr = $ this-> fetchAll ("", $ result_type );
If (is_array ($ arr )){
Return $ arr [0];
}
} Else {
Return false;
}
}
Function fetchall ($ primarykey = "", $ result_type = mysql_assoc)
{
If ($ this-> _ result)
{
$ I = 0;
$ This-> _ rowset = array ();
If ($ primarykey = "")
{
While ($ this-> next ($ result_type ))
{
$ This-> _ rowset [$ I] = $ this-> _ record;
$ I ++;
}
} Else {
While ($ this-> next ($ result_type ))
{
$ This-> _ rowset [$ this-> f ($ primaryKey)] = $ this-> _ record;
$ I ++;
}
}
Return $ this-> _ rowset;
} Else {
// $ This-> _ halt ("Invalid Result ");
Return false;
}
}
Function checkExist ($ SQL)
{
Return $ this-> query ($ SQL)> 0? True: false;
}
Function getvalue ($ SQL, $ colset = 0)
{
If ($ this-> query ($ SQL)> 0 ){
$ This-> next (mysql_both );
Return $ this-> F ($ colset );
} Else {
Return false;
}
}
Function getnumrows ()
{
Return @ mysql_num_rows ($ this-> _ result );
}
Function getnumfields ()
{
Return @ mysql_num_fields ($ this-> _ result );
}
Function getfiledname ($ offset)
{
Return @ mysql_field_name ($ this-> _ result, $ offset );
}
Function getfiledtype ($ offset)
{
Return @ mysql_field_type ($ this-> _ result, $ offset );
}
Function getfiledlen ($ offset)
{
Return @ mysql_field_len ($ this-> _ result, $ offset );
}
Function getinsertid ()
{
Return @ mysql_insert_id ($ this-> _ db_connect_id );
}
Function getaffectedrows ()
{
Return @ mysql_affected_rows ($ this-> _ db_connect_id );
}
Function free_result ()
{
$ Ret = @ mysql_free_result ($ this-> _ result );
$ This-> _ result = 0;
Return $ ret;
}
Function version (){
Return @ mysql_get_server_info ($ this-> _ db_connect_id );
}
Function close (){
Return @ mysql_close ($ this-> _ db_connect_id );
}
Function sqlOutput ($ isOut = true, $ all = true ){
If ($ all ){
$ Ret = implode ("<br>", $ this-> _ SQL );
} Else {
$ Ret = $ this-> _ SQL [count ($ this-> _ SQL)-1];
}
If ($ isOut ){
Echo $ ret;
} Else {
Return $ ret;
}
}
Function _ halt ($ MSG = "session halted.", $ geterr = true ){
If ($ this-> _ isdebug ){
If ($ geterr ){
$ This-> _ errno = @ mysql_errno ($ this-> _ db_connect_id );
$ This-> _ error = @ mysql_error ($ this-> _ db_connect_id );
Printf ("<B> MySQL _ error </B>: % s (% s) <br> </font>/N", $ this-> _ errno, $ this-> _ error );
}
Die ($ MSG );
} Else {
Die ("Session halted .");
}
}
}
?>