php数据库连接类
[PHP]代码
<?php /* * 名称: 数据库链接类 * 介绍: 适用于各种数据库链接 * 作者: hetaoren <452649510@qq.com> * 创建时间: 2010-12-07 * 最后修改: 2010-12-08 */ class mysql { private $_link; public function __construct($dbhost='localhost',$dbuser='root',$dbpassword='',$dbname='taojindidai',$charset='gbk') { $this->_link = mysql_connect($dbhost,$dbuser,$dbpassword,true); /*连接数据库*/ $this->_link or $this->errmsg('无法连接MYSQL服务器!'); /*是否连接成功*/ if ($this->version() > '4.1') { /*检查数据库版本*/ $this->query('set names '.$charset); /*设置数据库编码*/ } /*打开数据库*/ mysql_select_db($dbname,$this->_link) or $this->errmsg('无法连接数据库!'); } /*执行数据库操作*/ public function query($sql) { $result = mysql_query($sql,$this->_link); $result or $this->errmsg('执行SQL语句错误!'); 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 = '<strong>一个MySQL错误发生!</strong><br />'; $message .= '<strong>错误号:</strong>'. mysql_errno($this->_link) .'<br />'; $message .= '<strong>错误描述:</strong>'. $msg . mysql_error($this->_link) .'<br />'; $message .= '<strong>错误时间:</strong>'. date('Y-m-d H:i:s'); 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['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { $ip = "unknown"; $ip = $_SERVER['REMOTE_ADDR']; }else{ } return ($ip); } } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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

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
