Table of Contents
[PHP]代码
Home php教程 PHP源码 一个数据库操作类

一个数据库操作类

May 25, 2016 pm 05:15 PM
php

[PHP]代码

<?php			//数据库操作类

if(!defined(&#39;SQL_LAYER&#39;))
{
	define(&#39;SQL_LAYER&#39;,"mysql");

	class sql_db
	{
		var $db_connect_id;
		var $query_result;
		var $row=array();
		var $rowset=array();
		var $num_queries=0;

		function __construct($sqlserver,$sqluser,$sqlpassword,$database,$persistency=true)
		{
			$this->persistency=$persistency;
			$this->user=$sqluser;
			$this->password=$sqlpassword;
			$this->server=$sqlserver;
			$this->dbname=$database;

			if($this->persistency)
			{
				$this->db_connect_id=mysql_pconnect($this->server,$this->user,$this->password);
			}
			else
			{
				$this->db_connect_id=mysql_connect($this->server,$this->user,$this->password);
			}
			if($this->db_connect_id)
			{
				if($database!="")
				{
					$this->dbname=$database;
					$dbselect=mysql_select_db($this->dbname);
					if(!$dbselect)
					{
						mysql_close($this->db_connect_id);
						$this->db_connect_id=$dbselect;
					}
				}
				return $this->db_connect_id;

			}
			else
			{
				return false;
			}
		}

		//其他基本方法
		function sql_close()//关闭数据库
		{
			if($this->db_connect_id)
			{
				if($this->query_result)
				{
					//mysql_free_result() 仅需要在考虑到返回很大的结果集时会占用多少内存时调用。
					//在脚本结束后所有关联的内存都会被自动释放。
					mysql_free_result($this->query_result);
				}

				//关闭数据库
				$result=mysql_close($this->db_connect_id);
				return $result;
			}
			else
			{
				return false;
			}
		}

		//SQL语句执行函数
		function sql_query($query="",$transaction=FALSE)
		{
			//释放内存
			unset($this->query_result);
			if($query!="")
			{
				$this->num_queries++;
				$this->query_result=mysql_query($query,$this->db_connect_id);
			}
			if($this->query_result)
			{
				//如果存在查询结果,则释放内存
				unset($this->row[$this->query_result]);
				unset($this->rowset[$this->query_result]);
				return $this->query_result;
			}
			else
			{
				return ($transaction==END_TRANSACTION) ? true : false;
			}
		}

		//定义获取数据的行数
		function sql_numrows($query_id=0)
		{
			if(!$query_id)
			{
				$query_id=$this->query_result;
			}
			if($query_id)
			{
				$result=mysql_num_rows($query_id);
				return $result;
			}
			else
			{
				return false;
			}
		}

		//取得前一次 MySQL 操作所影响的记录行数
		function sql_affectedrows()
		{
			if($this->db_connect_id)
			{
				$result=mysql_affected_rows($this->db_connect_id);
				return $result;
			}
			else
			{
				return false;
			}
		}

		//取得结果集中字段的数目
		function sql_numfields($query_id=0)
		{
			if(!$query_id)
			{
				$query_id=$this->query_result;
			}
			if($query_id)
			{
				$result=mysql_num_fields($query_id);
				return $result;
			}
			else
			{
				return false;
			}
		}

		//获取结果中指定字段的字段名
		function sql_fieldname($offset,$query_id=0)
		{
			if(!$query_id)
			{
				$query_id=$this->query_result;
			}
			if($query_id)
			{
				$result=mysql_field_name($query_id,$offset);
				return $result;
			}
			else
			{
				return false;
			}

		}

		//获取结果集中指定字段的类型
		function sql_fieldtype($offset,$query_id=0)
		{
			if(!$query_id)
			{
				$query_id=$this->query_result;
			}
			if($query_id)
			{
				$result=mysql_field_type($query_id,$offset);
				return $result;
			}
			else
			{
				return false;
			}
		}

		function sql_fetchrow($query_id=0)
		{
			if(!$query_id)
			{
				$query_id=$this->query_result;
			}
			if($query_id)
			{
				$this->row[$query_id]=mysql_fetch_array($query_id);
				return $this->row[$query_id];
			}
			else
			{
				return false;
			}
		}

		function sql_fetchrowset($query_id = 0)
		{
			if(!$query_id)
			{
				$query_id = $this->query_result;
			}
			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;
			}
		}

		function sql_fetchfield($field, $rownum = -1, $query_id = 0)
		{
			if(!$query_id)
			{
				$query_id = $this->query_result;
			}
			if($query_id)
			{
				if($rownum > -1)
				{
					//取得结果的值
					$result = @mysql_result($query_id, $rownum, $field);
				}
				else
				{
					if(empty($this->row[$query_id]) && empty($this->rowset[$query_id]))
					{
						if($this->sql_fetchrow())
						{
							$result = $this->row[$query_id][$field];
						}
					}
					else
					{
						if($this->rowset[$query_id])
						{
							$result = $this->rowset[$query_id][0][$field];
						}
						else if($this->row[$query_id])
						{
							$result = $this->row[$query_id][$field];
						}
					}
				}
				return $result;
			}
			else
			{
				return false;
			}
		}

		//行选择函数
		function sql_rowseek($rownum, $query_id = 0)
		{
			if(!$query_id)
			{	//获取$query_id的值
				$query_id = $this->query_result;
			}
			if($query_id)
			{
				//移动内部结果的指针
				$result = @mysql_data_seek($query_id, $rownum);
				return $result;
			}
			else
			{
				return false;
			}
		}

		function sql_nextid()
		{
			if($this->db_connect_id)
			{	//取得上一步 INSERT 操作产生的 ID
				$result = @mysql_insert_id($this->db_connect_id);
				return $result;
			}
			else
			{
				return false;
			}
		}

		function sql_freeresult($query_id=0)
		{
			if(!$query_id)
			{
				$query_id=$this->query_result;
			}
			if($query_id)
			{
				//释放给定的变量的内存
				unset($this->row[$query_id]);
				unset($this->rowset[$query_id]);
				//释放内存
				mysql_free_result($query_id);
				return true;
			}
			else
			{
				return false;
			}
		}

		//SQL语句执行错误的函数
		function sql_error($query_id=0)
		{
			$result["message"]=mysql_error($this->db_connect_id);

			$result["code"]=mysql_errno($this->db_connect_id);
			return $result;
		}


	}
}

?>
Copy after login

                   

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles