MySQL Class
作者: Syber 日期: 2008-08-01 00:45
一个MySQL操作类
- <?
- /***************************************************************************
- * Mysql.Class.php
- * -------------------------
- * begin : Thursday, Apr 27, 2006
- * copyright : (C) 2002 The ReQi.com
- * email : maxflg@yahoo.com
- *
- * $Id: Mysql.Class.php,v 1.0.2.1 2006/04/27 16:23:20 Syber Exp $
- *
- ***************************************************************************/
- /***************************************************************************
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- ***************************************************************************/
- /***************************************************************************
- *
- * Thanks my love,my all,my darling.
- *
- ***************************************************************************/
- /***************************************************************************
- *
- * Example:
- * $mysql = new MySql_Class( 'dbuser', 'dbpassword', 'dbname', 'dbhost' );
- * $mysql -> DBConnect();
- * $mysql -> Post( 'UPDATE $table SET $column = $value WHERE $Expr' );
- * $row = $mysql -> Open( 'SELECT $columns FROM $table WHERE $Expr' );
- * $rows = $mysql -> Open( 'SELECT $columns FROM $table WHERE $Expr', True, True );
- *
- ***************************************************************************/
- Class MySql_Class
- {
- var $Conn;
- var $DbUser;
- var $DbPass;
- var $DbName;
- var $DbHost;
- var $DbResult;
- var $row = array();
- var $rowset = array();
- var $num_queries = 0;
- Function MySql_Class( $user, $passwd, $db, $host )
- {
- $this -> DbUser = $user;
- $this -> DbPass = $passwd;
- $this -> DbName = $db;
- $this -> DbHost = $host;
- } // end Function
- //
- // Constructor
- //
- Function DBConnect( $persistency = false )
- {
- if( $persistency )
- {
- $this -> Conn = @mysql_pconnect( $this -> DbHost, $this -> DbUser, $this -> DbPass );
- }
- else
- {
- $this->Conn = @mysql_connect( $this -> DbHost, $this -> DbUser, $this -> DbPass );
- }
- if( $this -> Conn )
- {
- if( DbName != "" )
- {
- $dbselect = @mysql_select_db( $this -> DbName );
- if( !$dbselect )
- {
- @mysql_close( $this -> Conn );
- $this -> Conn = $dbselect;
- }
- }
- return $this -> Conn;
- }
- else
- {
- return false;
- }
- } // end Function
- Function DBClose()
- {
- if( $this -> Conn )
- {
- if( $this -> DbResult )
- {
- @mysql_free_result( $this -> DbResult );
- }
- $result = @mysql_close( $this -> Conn );
- return $result;
- }
- else
- {
- return false;
- }
- } // end Function
- //
- // Base query method
- // When $Affectedrows = true,then return Affectedrows
- //
- Function Post( $sql, $Affectedrows = false )
- {
- unset( $this -> DbResult );
- if( $sql != "" )
- {
- $this -> num_queries++;
- $this -> DbResult = @mysql_query( $sql, $this -> Conn );
- }
- if( $this -> DbResult )
- {
- unset( $this -> row[ $this -> DbResult ] );
- unset( $this -> rowset[ $this -> DbResult ] );
- return $Affectedrows ? $this -> AffectedRows() : $this -> DbResult;
- }
- else
- {
- return false;
- }
- } // end Function
- //
- // Base query method
- // $MuchRows, get one row of false, much rows of true.
- // $OutputFetch, get field row('s) of true.
- //
- Function Open( $sql, $MuchRows = false, $OutputFetch = false )
- {
- if ( $this -> Post( $sql ) )
- {
- if ( $MuchRows )
- {
- if ( $OutputFetch )
- {
- return $this -> SQL_FetchRowSet();
- }
- else
- {
- return $this -> SQL_ArrayRowSet();
- }
- }
- else
- {
- if ( $OutputFetch )
- {
- return $this -> SQL_FetchRow();
- }
- else
- {
- return $this -> SQL_ArrayRow();
- }
- }
- }
- else
- {
- return false;
- }
- } // end Function
- Function SQL_FetchRow( $query_id = 0 )
- {
- if( !$query_id )
- {
- $query_id = $this -> DbResult;
- }
- if( $query_id )
- {
- $this -> row[ $query_id ] = @mysql_fetch_array( $query_id );
- return $this -> row[ $query_id ];
- }
- else
- {
- return false;
- }
- } // end Function
- Function SQL_FetchRowSet( $query_id = 0 )
- {
- if( !$query_id )
- {
- $query_id = $this -> DbResult;
- }
- if( $query_id )
- {
- unset( $this -> rowset[ $query_id ] );
- unset( $this -> row[ $query_id ] );
- while( $this -> rowset[ $query_id ] = @mysql_fetch_array( $query_id ) )
- {
- $result[] = $this->rowset[ $query_id ];
- }
- return $result;
- }
- else
- {
- return false;
- }
- } // end Function
- Function SQL_ArrayRow( $query_id = 0 )
- {
- if( !$query_id )
- {
- $query_id = $this -> DbResult;
- }
- if( $query_id )
- {
- $this -> row[ $query_id ] = @mysql_fetch_row( $query_id );
- return $this -> row[ $query_id ];
- }
- else
- {
- return false;
- }
- } // end Function
- Function SQL_ArrayRowSet( $query_id = 0 )
- {
- if( !$query_id )
- {
- $query_id = $this -> DbResult;
- }
- if( $query_id )
- {
- unset( $this -> rowset[ $query_id ] );
- unset( $this -> row[ $query_id ] );
- while( $this -> rowset[ $query_id ] = @mysql_fetch_row( $query_id ) )
- {
- $result[] = $this->rowset[ $query_id ];
- }
- return $result;
- }
- else
- {
- return false;
- }
- } // end Function
- Function AffectedRows()
- {
- if( $this -> Conn )
- {
- $result = @mysql_affected_rows( $this -> Conn );
- return $result;
- }
- else
- {
- return false;
- }
- } // end Function
- Function SQL_NextID()
- {
- if( $this -> Conn)
- {
- $result = @mysql_insert_id( $this -> Conn );
- return $result;
- }
- else
- {
- return false;
- }
- } // end Function
- Function SQL_FreeResult( $query_id = 0 )
- {
- if( !$query_id )
- {
- $query_id = $this -> DbResult;
- }
- if ( $query_id )
- {
- unset( $this -> row[ $query_id ] );
- unset( $this -> rowset[ $query_id ] );
- @mysql_free_result( $query_id );
- return true;
- }
- else
- {
- return false;
- }
- } // end Function
- Function SQL_Error($query_id = 0)
- {
- $result[ "message" ] = @mysql_error( $this -> Conn);
- $result[ "code" ] = @mysql_errno( $this -> Conn);
- return $result;
- } // end Function
- } // end Class
- ?>
加入网摘
订阅
上一篇
返回
下一篇
