首页 后端开发 php教程 实用mysql数据库连接类_PHP教程

实用mysql数据库连接类_PHP教程

Jul 13, 2016 pm 05:05 PM
mysql php 代码 实用 数据库 文件 连接

这是一款php中的mysql数据库连接文件代码,如果你正在找这样功能的代码,可以进来看看,非常完整文件

 这是一款php教程中的mysql教程数据库教程连接文件代码,如果你正在找这样功能的代码,可以进来看看,非常完整文件。*/

class mysql {
 private $db_host;     //主机地址
 private $db_user;     //用户名
 private $db_pass;     //连接密码
 private $db_name;     //名称
 private $db_charset;  //编码

 private $conn;
 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 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 show_error ($msg) {
 $errinfo = mysql_error();
 echo "错误:$msg
返回:$errinfo

";
 }

// 释放内存
 public function free_result () {
 if ( @mysql_free_result($this->query_id) )
 unset ($this->result);
 $this->query_id = 0;
 }

} // end class

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/630774.htmlTechArticle这是一款php中的mysql数据库连接文件代码,如果你正在找这样功能的代码,可以进来看看,非常完整文件 这是一款php教程中的mysql教程数据...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PHP的目的:构建动态网站 PHP的目的:构建动态网站 Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP行动:现实世界中的示例和应用程序 PHP行动:现实世界中的示例和应用程序 Apr 14, 2025 am 12:19 AM

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP和Python:代码示例和比较 PHP和Python:代码示例和比较 Apr 15, 2025 am 12:07 AM

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP:处理数据库和服务器端逻辑 PHP:处理数据库和服务器端逻辑 Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

docker怎么启动mysql docker怎么启动mysql Apr 15, 2025 pm 12:09 PM

在 Docker 中启动 MySQL 的过程包含以下步骤:拉取 MySQL 镜像创建并启动容器,设置根用户密码并映射端口验证连接创建数据库和用户授予对数据库的所有权限

为什么要使用PHP?解释的优点和好处 为什么要使用PHP?解释的优点和好处 Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

PHP与Python:用例和应用程序 PHP与Python:用例和应用程序 Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

See all articles