首頁 後端開發 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)

laravel入門實例 laravel入門實例 Apr 18, 2025 pm 12:45 PM

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

甲骨文在商業世界中的作用 甲骨文在商業世界中的作用 Apr 23, 2025 am 12:01 AM

Oracle不僅是數據庫公司,還是雲計算和ERP系統的領導者。 1.Oracle提供從數據庫到雲服務和ERP系統的全面解決方案。 2.OracleCloud挑戰AWS和Azure,提供IaaS、PaaS和SaaS服務。 3.Oracle的ERP系統如E-BusinessSuite和FusionApplications幫助企業優化運營。

laravel框架安裝方法 laravel框架安裝方法 Apr 18, 2025 pm 12:54 PM

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

MySQL和PhpMyAdmin:核心功能和功能 MySQL和PhpMyAdmin:核心功能和功能 Apr 22, 2025 am 12:12 AM

MySQL和phpMyAdmin是強大的數據庫管理工具。 1)MySQL用於創建數據庫和表、執行DML和SQL查詢。 2)phpMyAdmin提供直觀界面進行數據庫管理、表結構管理、數據操作和用戶權限管理。

繼續使用PHP:耐力的原因 繼續使用PHP:耐力的原因 Apr 19, 2025 am 12:23 AM

PHP仍然流行的原因是其易用性、靈活性和強大的生態系統。 1)易用性和簡單語法使其成為初學者的首選。 2)與web開發緊密結合,處理HTTP請求和數據庫交互出色。 3)龐大的生態系統提供了豐富的工具和庫。 4)活躍的社區和開源性質使其適應新需求和技術趨勢。

MySQL與其他編程語言:一種比較 MySQL與其他編程語言:一種比較 Apr 19, 2025 am 12:22 AM

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。MySQL以其高性能、可扩展性和跨平台支持著称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

如何安全地將包含函數和正則表達式的JavaScript對象存儲到數據庫並恢復? 如何安全地將包含函數和正則表達式的JavaScript對象存儲到數據庫並恢復? Apr 19, 2025 pm 11:09 PM

安全地處理JSON中的函數和正則表達式在前端開發中,經常需要將JavaScript...

IIS和PHP的兼容性:深度潛水 IIS和PHP的兼容性:深度潛水 Apr 22, 2025 am 12:01 AM

IIS和PHP可以兼容,通過FastCGI實現。 1.IIS通過配置文件將.php文件請求轉發給FastCGI模塊。 2.FastCGI模塊啟動PHP進程處理請求,提高性能和穩定性。 3.實際應用中需注意配置細節、錯誤調試和性能優化。

See all articles