codetc - 网站开发技术 首页 代码 PHP 查看内容

实用的PHP MySQL工具类

2014-12-24 00:21| 发布者: CODETC| 查看: 2126| 评论: 0

简单实用的PHP MySQL工具类,实现常用的增、删、改、查等操作,可控制返回的数据格式和记录条数,支持使用持久连接。

文件下载:mysql.class.php(5.45kb)

<?php
/***************************************************************************
 *
 *   PHP MySQL数据库工具类
 *   使用例子:
 *   $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 $DbPort;
	var $DbResult;
	var $row = array();
	var $rowset = array();
	var $num_queries = 0;

	Function MySql_Class( $user, $passwd, $db, $host, $port = 3306 )
	{
		$this -> DbUser = $user;
		$this -> DbPass = $passwd;
		$this -> DbName = $db;
		$this -> DbHost = $host;
		$this -> DbPort = $port;
	}

	/**
	 * 连接数据库
	 * $persistency 是否使用持久链接
	 */
	Function DBConnect( $persistency = false )
	{
		if( $persistency )
		{
			$this -> Conn = @mysql_pconnect( $this -> DbHost . ':' . $this -> DbPort, $this -> DbUser, $this -> DbPass );
		}
		else
		{
			$this->Conn = @mysql_connect( $this -> DbHost . ':' . $this -> DbPort, $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;
		}
	}

	/**
	 * 关闭数据库连接
	 */
	Function DBClose()
	{
		if( $this -> Conn )
		{
			if( $this -> DbResult )
			{
				@mysql_free_result( $this -> DbResult );
			}
			$result = @mysql_close( $this -> Conn );
			return $result;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 更新查询方法
	 * 当 $Affectedrows = true 时返回影响行数
	 */
	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;
		}
	}

	/**
	 * 基本查询方法
	 * $MuchRows 获取1条记录时为false,等于true时返回多条记录
	 * $OutputFetch 返回值包括列名和索引两种结构
	 */
	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

}
?>
文章来源 CODETC,欢迎分享,转载请注明地址: http://www.codetc.com/article-102-1.html

最新评论

 作为游客发表评论,请输入您的昵称

返回顶部