侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php mysql 封装类实例代码

php  /  管理员 发布于 5年前   495

废话不多说了,具体代码如下所示:

db_host = $db_host;$this->db_user = $db_user;$this->db_pwd = $db_pwd;$this->db_database = $db_database;$this->conn = $conn;$this->coding = $coding;$this->connect();}/*数据库连接*/public function connect() {if ($this->conn == "pconn") {//永久链接$this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);} else {//即使链接$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);}if (!mysql_select_db($this->db_database, $this->conn)) {if ($this->show_error) {$this->show_error("数据库不可用:", $this->db_database);}}mysql_query("SET NAMES $this->coding");}/*数据库执行语句,可执行查询添加修改删除等任何sql语句*/public function query($sql) {if ($sql == "") {$this->show_error("SQL语句错误:", "SQL查询语句为空");}$this->sql = $sql;$result = mysql_query($this->sql, $this->conn);if (!$result) {//调试中使用,sql语句出错时会自动打印出来if ($this->show_error) {$this->show_error("错误SQL语句:", $this->sql);}} else {$this->result = $result;}return $this->result;}/*创建添加新的数据库*/public function create_database($database_name) {$database = $database_name;$sqlDatabase = 'create database ' . $database;$this->query($sqlDatabase);}/*查询服务器所有数据库*///将系统数据库与用户数据库分开,更直观的显示?public function show_databases() {$this->query("show databases");echo "现有数据库:" . $amount = $this->db_num_rows($rs);echo "
";$i = 1;while ($row = $this->fetch_array($rs)) {echo "$i $row[Database]";echo "
";$i++;}}//以数组形式返回主机中所有数据库名public function databases() {$rsPtr = mysql_list_dbs($this->conn);$i = 0;$cnt = mysql_num_rows($rsPtr);while ($i < $cnt) {$rs[] = mysql_db_name($rsPtr, $i);$i++;}return $rs;}/*查询数据库下所有的表*/public function show_tables($database_name) {$this->query("show tables");echo "现有数据库:" . $amount = $this->db_num_rows($rs);echo "
";$i = 1;while ($row = $this->fetch_array($rs)) {$columnName = "Tables_in_" . $database_name;echo "$i $row[$columnName]";echo "
";$i++;}}/*mysql_fetch_row() array $row[0],$row[1],$row[2]mysql_fetch_array() array $row[0] 或 $row[id]mysql_fetch_assoc() array 用$row->content 字段大小写敏感mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感*//*取得结果数据*/public function mysql_result_li() {return mysql_result($str);}/*取得记录集,获取数组-索引和关联,使用$row['content'] */public function fetch_array($resultt="") {if($resultt<>""){return mysql_fetch_array($resultt);}else{return mysql_fetch_array($this->result);}}//获取关联数组,使用$row['字段名']public function fetch_assoc() {return mysql_fetch_assoc($this->result);}//获取数字索引数组,使用$row[0],$row[1],$row[2]public function fetch_row() {return mysql_fetch_row($this->result);}//获取对象数组,使用$row->contentpublic function fetch_Object() {return mysql_fetch_object($this->result);}//简化查询selectpublic function findall($table) {$this->query("SELECT * FROM $table");}//简化查询selectpublic function select($table, $columnName = "*", $condition = '', $debug = '') {$condition = $condition ? ' Where ' . $condition : NULL;if ($debug) {echo "SELECT $columnName FROM $table $condition";} else {$this->query("SELECT $columnName FROM $table $condition");}}//简化删除delpublic function delete($table, $condition, $url = '') {if ($this->query("DELETE FROM $table WHERE $condition")) {if (!empty ($url))$this->Get_admin_msg($url, '删除成功!');}}//简化插入insertpublic function insert($table, $columnName, $value, $url = '') {if ($this->query("INSERT INTO $table ($columnName) VALUES ($value)")) {if (!empty ($url))$this->Get_admin_msg($url, '添加成功!');}}//简化修改updatepublic function update($table, $mod_content, $condition, $url = '') {//echo "UPDATE $table SET $mod_content WHERE $condition"; exit();if ($this->query("UPDATE $table SET $mod_content WHERE $condition")) {if (!empty ($url))$this->Get_admin_msg($url);}}/*取得上一步 INSERT 操作产生的 ID*/public function insert_id() {return mysql_insert_id();}//指向确定的一条数据记录public function db_data_seek($id) {if ($id > 0) {$id = $id -1;}if (!@ mysql_data_seek($this->result, $id)) {$this->show_error("SQL语句有误:", "指定的数据为空");}return $this->result;}// 根据select查询结果计算结果集条数public function db_num_rows() {if ($this->result == null) {if ($this->show_error) {$this->show_error("SQL语句错误", "暂时为空,没有任何内容!");}} else {return mysql_num_rows($this->result);}}// 根据insert,update,delete执行结果取得影响行数public function db_affected_rows() {return mysql_affected_rows();}//输出显示sql语句public function show_error($message = "", $sql = "") {if (!$sql) {echo "" . $message . "";echo "
";} else {echo "
";echo "错误信息提示:
";echo "
";echo "
";echo "错误号:12142";echo "
";echo "错误原因:" . mysql_error() . "

";echo "
";echo "" . $message . "";echo "";echo "
" . $sql . "
";$ip = $this->getip();if ($this->bulletin) {$time = date("Y-m-d H:i:s");$message = $message . "\r\n$this->sql" . "\r\n客户IP:$ip" . "\r\n时间 :$time" . "\r\n\r\n";$server_date = date("Y-m-d");$filename = $server_date . ".txt";$file_path = "error/" . $filename;$error_content = $message;//$error_content="错误的数据库,不可以链接";$file = "error"; //设置文件保存目录//建立文件夹if (!file_exists($file)) {if (!mkdir($file, 0777)) {//默认的 mode 是 0777,意味着最大可能的访问权die("upload files directory does not exist and creation failed");}}//建立txt日期文件if (!file_exists($file_path)) {//echo "建立日期文件";fopen($file_path, "w+");//首先要确定文件存在并且可写if (is_writable($file_path)) {//使用添加模式打开$filename,文件指针将会在文件的开头if (!$handle = fopen($file_path, 'a')) {echo "不能打开文件 $filename";exit;}//将$somecontent写入到我们打开的文件中。if (!fwrite($handle, $error_content)) {echo "不能写入到文件 $filename";exit;}//echo "文件 $filename 写入成功";echo "――错误记录被保存!";//关闭文件fclose($handle);} else {echo "文件 $filename 不可写";}} else {//首先要确定文件存在并且可写if (is_writable($file_path)) {//使用添加模式打开$filename,文件指针将会在文件的开头if (!$handle = fopen($file_path, 'a')) {echo "不能打开文件 $filename";exit;}//将$somecontent写入到我们打开的文件中。if (!fwrite($handle, $error_content)) {echo "不能写入到文件 $filename";exit;}//echo "文件 $filename 写入成功";echo "――错误记录被保存!";//关闭文件fclose($handle);} else {echo "文件 $filename 不可写";}}}echo "
";if ($this->is_error) {exit;}}echo "";echo "
";echo "
";}//释放结果集public function free() {@ mysql_free_result($this->result);}//数据库选择public function select_db($db_database) {return mysql_select_db($db_database);}//查询字段数量public function num_fields($table_name) {//return mysql_num_fields($this->result);$this->query("select * from $table_name");echo "
";echo "字段数:" . $total = mysql_num_fields($this->result);echo "
";for ($i = 0; $i < $total; $i++) {print_r(mysql_fetch_field($this->result, $i));}echo "
";echo "
";}//取得 MySQL 服务器信息public function mysql_server($num = '') {switch ($num) {case 1 :return mysql_get_server_info(); //MySQL 服务器信息break;case 2 :return mysql_get_host_info(); //取得 MySQL 主机信息break;case 3 :return mysql_get_client_info(); //取得 MySQL 客户端信息break;case 4 :return mysql_get_proto_info(); //取得 MySQL 协议信息break;default :return mysql_get_client_info(); //默认取得mysql版本信息}}//析构函数,自动关闭数据库,垃圾回收机制public function __destruct() {if (!empty ($this->result)) {$this->free();}mysql_close($this->conn);} //function __destruct();/*获得客户端真实的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 = $_SERVER['REMOTE_ADDR'];} else {$ip = "unknown";}return ($ip);}function inject_check($sql_str) { //防止注入$check = eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str);if ($check) {echo "输入非法注入内容!";exit ();} else {return $sql_str;}}function checkurl() { //检查来路if (preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) !== preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) {header("Location: http://www.dareng.com");exit();}}}?>

以上所述是小编给大家介绍的php mysql 封装类实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对站的支持!

您可能感兴趣的文章:

  • PostgreSQL管理工具phpPgAdmin入门指南
  • php连接与操作PostgreSQL数据库的方法
  • PHP 读取Postgresql中的数组
  • PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例
  • php基于PDO实现功能强大的MYSQL封装类实例
  • php实现mysql封装类示例
  • PHP访问MYSQL数据库封装类(附函数说明)
  • PHP数据库处理封装类实例
  • PHP数据库表操作的封装类及用法实例详解
  • PHP操作Postgresql封装类与应用完整实例


  • 上一条:
    php MessagePack介绍
    下一条:
    PHP 5.3 下载时 VC9、VC6、Thread Safe、Non Thread Safe的区别分析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客