Table of Contents
[PHP]代码  
Home php教程 PHP源码 php数据库连接类

php数据库连接类

May 25, 2016 pm 05:15 PM
php

[PHP]代码  

<?php  
	/*
	* 名称: 数据库链接类
	* 介绍: 适用于各种数据库链接
	* 作者: hetaoren <452649510@qq.com>
	* 创建时间: 2010-12-07
	* 最后修改: 2010-12-08
	*/

	class mysql {  
		private $_link;  
		
		public function __construct($dbhost=&#39;localhost&#39;,$dbuser=&#39;root&#39;,$dbpassword=&#39;&#39;,$dbname=&#39;taojindidai&#39;,$charset=&#39;gbk&#39;) {  
			$this->_link = mysql_connect($dbhost,$dbuser,$dbpassword,true);  /*连接数据库*/ 
			$this->_link or $this->errmsg(&#39;无法连接MYSQL服务器!&#39;);  /*是否连接成功*/ 
			if ($this->version() > &#39;4.1&#39;) {                  /*检查数据库版本*/ 
				$this->query(&#39;set names &#39;.$charset);         /*设置数据库编码*/ 
			}  
			/*打开数据库*/
			mysql_select_db($dbname,$this->_link) or $this->errmsg(&#39;无法连接数据库!&#39;);  
		}  
		
		/*执行数据库操作*/ 
		public function query($sql)	{                                                               
			$result = mysql_query($sql,$this->_link);  
			$result or $this->errmsg(&#39;执行SQL语句错误!&#39;);  
			return $result;  
		} 
		 
		/*返回根据从结果集取得的行生成的数组*/ 
		/*MYSQL_BOTH 得到一个同时包含关联和数字索引的数组 (如同 mysql_fetch_array())*/ 
		/*MYSQL_ASSOC 得到一个同时包含关联和数字索引的数组 (如同 mysql_fetch_assoc())*/ 
		/*MYSQL_NUM 得到一个同时包含关联和数字索引的数组 (如同 mysql_fetch_row())*/ 
		public function fetch_array($result,$type = MYSQL_ASSOC) {
			return mysql_fetch_array($result,$type);     
		} 
		 
		/*返回根据所取得的行生成的对象*/ 
		public function fetch_object($result) {  
			return mysql_fetch_object($result);  
		} 
		 
		/*取得前一次 MySQL 操作所影响的记录行数*/ 
		public function affected_rows() {  
			return mysql_affected_rows($this->_link);  
		}  
		
		/* 释放结果内存*/ 
		public function free_result($result) {  
			return mysql_free_result($result);   
		}  
		
		/* 取得结果集中行的数目*/ 
		public function num_rows($result) {  
			return mysql_num_rows($result);  
		}
		  
		/* 取得结果集中字段的数目*/ 
		public function num_fields($result) {  
			return mysql_num_fields($result);  
		}  
		
		/*取得上一步 INSERT 操作产生的 ID*/ 
		public function insert_id() {  
			return mysql_insert_id($this->_link);  
		} 
		 
		/* 发出mysql执行错误*/ 
		private function errmsg($msg) {  
			$message  = &#39;<strong>一个MySQL错误发生!</strong><br />&#39;;  
			$message .= &#39;<strong>错误号:</strong>&#39;. mysql_errno($this->_link) .&#39;<br />&#39;;  
			$message .= &#39;<strong>错误描述:</strong>&#39;. $msg . mysql_error($this->_link) .&#39;<br />&#39;;  
			$message .= &#39;<strong>错误时间:</strong>&#39;. date(&#39;Y-m-d H:i:s&#39;);  
			exit($message);  
		} 
		 
		/*返回连接的标识*/ 
		public function link_id() {  
			return $this->_link;  
		}
		  
		/*返回数据库服务器版本*/ 
		public function version() {  
			return mysql_get_server_info($this->_link);  
		}  
		
		/*获得客户端真实的IP地址*/ 
		function getip() {  
			if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {  
				$ip = getenv("HTTP_CLIENT_IP");  
			}elseif(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {  
				$ip = getenv("HTTP_X_FORWARDED_FOR");  
			}elseif(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {  
				$ip = getenv("REMOTE_ADDR");  
			}elseif(isset ($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], "unknown")) {  				$ip = "unknown";  

				$ip = $_SERVER[&#39;REMOTE_ADDR&#39;];  
			}else{  
			}  
			return ($ip);  
		}  
	} 
?>
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