一款php数据库连接类_PHP教程
本文章提供了二款数据库教程连接实例,主要是讲php教程 mysql教程数据相关操作,有需要的朋友可以使用看看。
class mysql {
private $db_host; //主机地址
private $db_user; //用户名
private $db_pass; //连接密码
private $db_name; //名称
private $db_charset; //编码
private $conn;
public $debug=false;//调试开关,默认关闭
private $query_id; //用于判断sql语句是否执行成功
private $result; //结果集
private $num_rows; //结果集中行的数目,仅对select有效
private $insert_id; //上一步 insert 操作产生的 id
// 构造/析构函数
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}
function __destruct () {
@mysql_close($this->conn);
}
// 连接/选择数据库
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('数据库-连接失败:用户名或密码错误!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("数据库-选择失败:数据库 $this->db_name 不可用");
}
mysql_query("set names $this->db_charset");
return $this->conn;
}
// query方法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("sql语句 "$sql" 执行时遇到错误");
return $this->query_id;
}
// 显示详细错误信息
public function show_error ($msg) {
if($this->debug){
$errinfo = mysql_error();
echo "错误:$msg
返回:$errinfo";
}else{
echo '出现错误!
';
}
}
// 获得query执行成功与否的信息
public function get_query_info($info){
if ($this->query_id) {
echo $info;
}
}
// 查询所有
public function findall ($table_name) {
$this->query("select * from $table_name");
}
// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}
// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}
public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}
public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}
// 获取 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}
// 获取 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}
// 显示共有多少张表
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表
";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]
";
$i ++;
}
}
}
// 显示共有多少个数据库
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共有数据库 ".$this->num_rows($this->query_id)." 个
";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[database]."
";
$i ++;
}
}
}
// 删除数据库:返回删除结果
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//默认删除当前数据库
$this->query("drop database $db_name");
}else {
$this->query("drop database $db_name");
}
if ($this->query_id) {
return "数据库 $db_name 删除成功";
}else {
$this->show_error("数据库 $db_name 删除失败");
}
}
// 删除数据表:返回删除结果
public function drop_table ($table_name) {
$this->query("drop table $table_name");
if ($this->query_id) {
return "数据表 $table_name 删除成功";
}else {
$this->show_error("数据表 $table_name 删除失败");
}
}
// 创建数据库
public function create_db ($db_name) {
$this->query("create database $db_name");
if($this->query_id){
return "数据库 $db_name 创建成功";
}else {
$this->show_error("数据库 $db_name 创建失败");
}
}
// 获取数据库版本
public function get_info(){
echo mysql_get_server_info();
}
// 释放内存
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}
} // end class
?>
下面提供一款自动选择数据库远程或本地连接代码
// 包含mysql操作类
include_once 'mysql.class.php';
// 本地mysql数据
$mysql_local_data = array('db_host'=>'localhost',
'db_user'=>'root',
'db_pass'=>'root',
'db_name'=>'test');
// 远程mysql数据
$mysql_remote_data = array('db_host'=>'61.183.41.178',
'db_user'=>'xxx',
'db_pass'=>'xxx',
'db_name'=>'xxx');
// 公用数据
$tb_prefix = 'php95_';
$db_charset = 'utf-8';
//本地连接成功则实例化本地mysql类,否则连接远程数据库并实例化mysql类
if (@mysql_connect($mysql_local_data[db_host], $mysql_local_data[db_user], $mysql_local_data[db_pass]))
$db = new mysql($db_host, $mysql_local_data[db_user], $mysql_local_data[db_pass], $mysql_local_data[db_name], $db_charset, $conn);
else
$db = new mysql($mysql_remote_data[db_host], $mysql_remote_data[db_user], $mysql_remote_data[db_pass], $mysql_remote_data[db_name], $db_charset, $conn);
$db->show_tables(); //测试:显示当前数据库下的所有表名
?>
假设我们要在test.php文件中操作虚拟主机的数据库,则首先要在本地调试,那么必然要连接本地、远程两个不同的数据库,问题:怎么让test.php自动识别当下该连接本地还是远程数据库呢?

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

MySQL 8.4(截至 2024 年的最新 LTS 版本)中引入的主要变化之一是默认情况下不再启用“MySQL 本机密码”插件。此外,MySQL 9.0完全删除了这个插件。 此更改会影响 PHP 和其他应用程序

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

2025年全球数字虚拟币交易平台竞争激烈,本文根据交易量、安全性、用户体验等指标,权威发布2025年全球十大数字虚拟币交易平台排行榜。OKX凭借强大的技术实力和全球化运营策略居首,Binance以高流动性和低费用紧随其后。Gate.io、Coinbase、Kraken等平台凭借各自优势稳居前列。榜单涵盖Huobi、KuCoin、Bitfinex、Crypto.com和Gemini等交易平台,各有特色,但投资需谨慎。选择平台需考虑安全性、流动性、费用、用户体验、币种选择及监管合规性等因素,理性投资

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

虚拟货币十大交易平台排行榜(2025年最新): 币安:全球龙头,高流动性,监管受关注。 OKX:庞大用户基数,支持多种币种,提供杠杆交易。 Gate.io:资深交易所,多种法币支付方式,提供多种交易对和投资产品。 Bitget:衍生品交易所,高流动性,低费用。 火币:老牌交易所,支持多种币种和交易对。 Coinbase:美国知名交易所,受监管严格。 Phemex等等。

十大数字货币交易平台:1. OKX,2. Binance,3. Gate.io,4. Huobi Global,5. Kraken,6. Coinbase,7. KuCoin,8. Bitfinex,9. Crypto.com,10. Gemini,这些交易所各具特色,用户可根据安全性、费用、币种选择、用户界面和客户支持等因素选择适合自己的平台。
