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

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

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

这是一款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
使用数据库存储会话的优点是什么?使用数据库存储会话的优点是什么?Apr 24, 2025 am 12:16 AM

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

您如何在PHP中实现自定义会话处理?您如何在PHP中实现自定义会话处理?Apr 24, 2025 am 12:16 AM

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

什么是会话ID?什么是会话ID?Apr 24, 2025 am 12:13 AM

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

您如何在无状态环境(例如API)中处理会议?您如何在无状态环境(例如API)中处理会议?Apr 24, 2025 am 12:12 AM

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

您如何防止与会议有关的跨站点脚本(XSS)攻击?您如何防止与会议有关的跨站点脚本(XSS)攻击?Apr 23, 2025 am 12:16 AM

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

您如何优化PHP会话性能?您如何优化PHP会话性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

什么是session.gc_maxlifetime配置设置?什么是session.gc_maxlifetime配置设置?Apr 23, 2025 am 12:10 AM

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

您如何在PHP中配置会话名?您如何在PHP中配置会话名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

See all articles

热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

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

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中