php操作mysql封裝,php封裝協議

本文目錄一覽:

求PHP資料庫封裝類操作代碼

?php

class MySQL{

private $host; //伺服器地址

private $name; //登錄賬號

private $pwd; //登錄密碼

private $dBase; //資料庫名稱

private $conn; //資料庫鏈接資源

private $result; //結果集

private $msg; //返回結果

private $fields; //返回欄位

private $fieldsNum; //返回欄位數

private $rowsNum; //返回結果數

private $rowsRst; //返回單條記錄的欄位數組

private $filesArray = array(); //返回欄位數組

private $rowsArray = array(); //返回結果數組

private $charset=’utf8′; //設置操作的字符集

private $query_count=0; //查詢結果次數

static private $_instance; //存儲對象

//初始化類

private function __construct($host=”,$name=”,$pwd=”,$dBase=”){

if($host != ”) $this-host = $host;

if($name != ”) $this-name = $name;

if($pwd != ”) $this-pwd = $pwd;

if($dBase != ”) $this-dBase = $dBase;

$this-init_conn();

}

//防止被克隆

private function __clone(){}

public static function getInstance($host=”,$name=”,$pwd=”,$dBase=”){

if(FALSE == (self::$_instance instanceof self)){

self::$_instance = new self($host,$name,$pwd,$dBase);

}

return self::$_instance;

}

public function __set($name,$value){

$this-$name=$value;

}

public function __get($name){

return $this-$name;

}

//鏈接資料庫

function init_conn(){

$this-conn=@mysql_connect($this-host,$this-name,$this-pwd) or die(‘connect db fail !’);

@mysql_select_db($this-dBase,$this-conn) or die(‘select db fail !’);

mysql_query(“set names “.$this-charset);

}

//查詢結果

function mysql_query_rst($sql){

if($this-conn == ”) $this-init_conn();

$this-result = @mysql_query($sql,$this-conn);

$this-query_count++;

}

//取得欄位數

function getFieldsNum($sql){

$this-mysql_query_rst($sql);

$this-fieldsNum = @mysql_num_fields($this-result);

}

//取得查詢結果數

function getRowsNum($sql){

$this-mysql_query_rst($sql);

if(mysql_errno() == 0){

return @mysql_num_rows($this-result);

}else{

return ”;

}

}

//取得記錄數組(單條記錄)

function getRowsRst($sql,$type=MYSQL_BOTH){

$this-mysql_query_rst($sql);

if(empty($this-result)) return ”;

if(mysql_error() == 0){

$this-rowsRst = mysql_fetch_array($this-result,$type);

return $this-rowsRst;

}else{

return ”;

}

}

//取得記錄數組(多條記錄)

function getRowsArray($sql,$type=MYSQL_BOTH){

!empty($this-rowsArray) ? $this-rowsArray=array() : ”;

$this-mysql_query_rst($sql);

if(mysql_errno() == 0){

while($row = mysql_fetch_array($this-result,$type)) {

$this-rowsArray[] = $row;

}

return $this-rowsArray;

}else{

return ”;

}

}

//更新、刪除、添加記錄數

function uidRst($sql){

if($this-conn == ”){

$this-init_conn();

}

@mysql_query($sql);

$this-rowsNum = @mysql_affected_rows();

if(mysql_errno() == 0){

return $this-rowsNum;

}else{

return ”;

}

}

//返回最近插入的一條資料庫的id值

function returnRstId($sql){

if($this-conn == ”){

$this-init_conn();

}

@mysql_query($sql);

if(mysql_errno() == 0){

return mysql_insert_id();

}else{

return ”;

}

}

//獲取對應的欄位值

function getFields($sql,$fields){

$this-mysql_query_rst($sql);

if(mysql_errno() == 0){

if(mysql_num_rows($this-result) 0){

$tmpfld = @mysql_fetch_row($this-result);

$this-fields = $tmpfld[$fields];

}

return $this-fields;

}else{

return ”;

}

}

//錯誤信息

function msg_error(){

if(mysql_errno() != 0) {

$this-msg = mysql_error();

}

return $this-msg;

}

//釋放結果集

function close_rst(){

mysql_free_result($this-result);

$this-msg = ”;

$this-fieldsNum = 0;

$this-rowsNum = 0;

$this-filesArray = ”;

$this-rowsArray = ”;

}

//關閉資料庫

function close_conn(){

$this-close_rst();

mysql_close($this-conn);

$this-conn = ”;

}

//取得資料庫版本

function db_version() {

return mysql_get_server_info();

}

}

PHP訪問MYSQL資料庫封裝類(附函數說明)

複製代碼

代碼如下:

?php

/*

MYSQL

資料庫訪問封裝類

MYSQL

數據訪問方式,php4支持以mysql_開頭的過程訪問方式,php5開始支持以mysqli_開頭的過程和mysqli面向對象

訪問方式,本封裝類以mysql_封裝

數據訪問的一般流程:

1,連接資料庫

mysql_connect

or

mysql_pconnect

2,選擇資料庫

mysql_select_db

3,執行SQL查詢

mysql_query

4,處理返回的數據

mysql_fetch_array

mysql_num_rows

mysql_fetch_assoc

mysql_fetch_row

etc

*/

class

db_mysql

{

var

$querynum

=

;

//當前頁面進程查詢資料庫的次數

var

$dblink

;

//資料庫連接資源

//鏈接資料庫

function

connect($dbhost,$dbuser,$dbpw,$dbname=”,$dbcharset=’utf-8′,$pconnect=0

,

$halt=true)

{

$func

=

empty($pconnect)

?

‘mysql_connect’

:

‘mysql_pconnect’

;

$this-dblink

=

@$func($dbhost,$dbuser,$dbpw)

;

if

($halt

!$this-dblink)

{

$this-halt(“無法鏈接資料庫!”);

}

//設置查詢字符集

mysql_query(“SET

character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary”,$this-dblink)

;

//選擇資料庫

$dbname

@mysql_select_db($dbname,$this-dblink)

;

}

//選擇資料庫

function

select_db($dbname)

{

return

mysql_select_db($dbname,$this-dblink);

}

//執行SQL查詢

function

query($sql)

{

$this-querynum++

;

return

mysql_query($sql,$this-dblink)

;

}

//返回最近一次與連接句柄關聯的INSERT,UPDATE

或DELETE

查詢所影響的記錄行數

function

affected_rows()

{

return

mysql_affected_rows($this-dblink)

;

}

//取得結果集中行的數目,只對select查詢的結果集有效

function

num_rows($result)

{

return

mysql_num_rows($result)

;

}

//獲得單格的查詢結果

function

result($result,$row=0)

{

return

mysql_result($result,$row)

;

}

//取得上一步

INSERT

操作產生的

ID,只對錶有AUTO_INCREMENT

ID的操作有效

function

insert_id()

{

return

($id

=

mysql_insert_id($this-dblink))

=

?

$id

:

$this-result($this-query(“SELECT

last_insert_id()”),

0);

}

//從結果集提取當前行,以數字為key表示的關聯數組形式返回

function

fetch_row($result)

{

return

mysql_fetch_row($result)

;

}

//從結果集提取當前行,以欄位名為key表示的關聯數組形式返回

function

fetch_assoc($result)

{

return

mysql_fetch_assoc($result);

}

//從結果集提取當前行,以欄位名和數字為key表示的關聯數組形式返回

function

fetch_array($result)

{

return

mysql_fetch_array($result);

}

//關閉鏈接

function

close()

{

return

mysql_close($this-dblink)

;

}

//輸出簡單的錯誤html提示信息並終止程序

function

halt($msg)

{

$message

=

“html\nhead\n”

;

$message

.=

“meta

content=’text/html;charset=gb2312’\n”

;

$message

.=

“/head\n”

;

$message

.=

“body\n”

;

$message

.=

“資料庫出錯:”.htmlspecialchars($msg).”\n”

;

$message

.=

“/body\n”

;

$message

.=

“/html”

;

echo

$message

;

exit

;

}

}

?

php實現mysql封裝類示例

php封裝mysql類

複製代碼

代碼如下:

?php

class

Mysql

{

private

$host;

private

$user;

private

$pwd;

private

$dbName;

private

$charset;

private

$conn

=

null;

public

function

__construct()

{

$this-host

=

‘localhost’;

$this-user

=

‘root’;

$this-pwd

=

‘root’;

$this-dbName

=

‘test’;

$this-connect($this-host,$this-user,$this-pwd);

$this-switchDb($this-dbName);

$this-setChar($this-charset);

}

//負責鏈接

private

function

connect($h,$u,$p)

{

$conn

=

mysql_connect($h,$u,$p);

$this-conn

=

$conn;

}

//負責切換資料庫

public

function

switchDb($db)

{

$sql

=

‘use’

.

$db;

$this-query($sql);

}

//負責設置字符集

public

function

setChar($char)

{

$sql

=

‘set

names’

.

$char;

$this-query($sql);

}

//負責發送sql查詢

public

function

query($sql)

{

return

mysql_query($sql,$this-conn);

}

//負責獲取多行多列的select結果

public

function

getAll($sql)

{

$list

=

array();

$rs

=

$this-query($sql);

if

(!$rs)

{

return

false;

}

while

($row

=

mysql_fetch_assoc($rs))

{

$list[]

=

$row;

}

return

$list;

}

public

function

getRow($sql)

{

$rs

=

$this-query($sql);

if(!$rs)

{

return

false;

}

return

mysql_fetch_assoc($rs);

}

public

function

getOne($sql)

{

$rs

=

$this-query($sql);

if

(!$rs)

{

return

false;

}

return

mysql_fetch_assoc($rs);

return

$row[0];

}

public

function

close()

{

mysql_close($this-conn);

}

}

echo

‘pre’;

$mysql

=

new

Mysql();

print_r($mysql);

$sql

=

“insert

into

stu

values

(4,’wangwu’,’99998′)”;

if($mysql-query($sql)){

echo

“query成功”;

}else

{

echo

“失敗”;

}

echo

“br

/”;

$sql

=

“select

*

from

stu”;

$arr

=

$mysql-getAll($sql);

print_r($arr);

?

php封裝好的mysql操作庫嗎

php封裝好的mysql操作庫

類文件mysql.class.php:

?php

class Mysql{

//資料庫連接返回值

private $conn;

/**

* [構造函數,返回值給$conn]

* @param [string] $hostname [主機名]

* @param [string] $username[用戶名]

* @param [string] $password[密碼]

* @param [string] $dbname[資料庫名]

* @param [string] $charset[字符集]

* @return [null]

*/

function __construct($hostname,$username,$password,$dbname,$charset=’utf8′){

$config = @mysql_connect($hostname,$username,$password);

if(!$config){

echo ‘連接失敗,請聯繫管理員’;

exit;

}

$this-conn = $config;

$res = mysql_select_db($dbname);

if(!$res){

echo ‘連接失敗,請聯繫管理員’;

exit;

}

mysql_set_charset($charset);

}

function __destruct(){

mysql_close();

}

/**

* [getAll 獲取所有信息]

* @param [string] $sql [sql語句]

* @return [array] [返回二維數組]

*/

function getAll($sql){

$result = mysql_query($sql,$this-conn);

$data = array();

if($result mysql_num_rows($result)0){

while($row = mysql_fetch_assoc($result)){

$data[] = $row;

}

}

return $data;

}

/**

* [getOne 獲取單條數據]

* @param [string] $sql [sql語句]

* @return [array] [返回一維數組]

*/

function getOne($sql){

$result = mysql_query($sql,$this-conn);

$data = array();

if($result mysql_num_rows($result)0){

$data = mysql_fetch_assoc($result);

}

return $data;

}

/**

* [getOne 獲取單條數據]

* @param [string] $table [表名]

* @param [string] $data [由欄位名當鍵,屬性當鍵值的一維數組]

* @return [type] [返回false或者插入數據的id]

*/

function insert($table,$data){

$str = ”;

$str .=”INSERT INTO `$table` “;

$str .=”(`”.implode(“`,`”,array_keys($data)).”`) “;

$str .=” VALUES “;

$str .= “(‘”.implode(“‘,'”,$data).”‘)”;

$res = mysql_query($str,$this-conn);

if($res mysql_affected_rows()0){

return mysql_insert_id();

}else{

return false;

}

}

/**

* [update 更新資料庫]

* @param [string] $table [表名]

* @param [array] $data [更新的數據,由欄位名當鍵,屬性當鍵值的一維數組]

* @param [string] $where [條件,『欄位名』=『欄位屬性』]

* @return [type] [更新成功返回影響的行數,更新失敗返回false]

*/

function update($table,$data,$where){

$sql = ‘UPDATE ‘.$table.’ SET ‘;

foreach($data as $key = $value){

$sql .= “`{$key}`='{$value}’,”;

}

$sql = rtrim($sql,’,’);

$sql .= ” WHERE $where”;

$res = mysql_query($sql,$this-conn);

if($res mysql_affected_rows()){

return mysql_affected_rows();

}else{

return false;

}

}

/**

* [delete 刪除數據]

* @param [string] $table [表名]

* @param [string] $where [條件,『欄位名』=『欄位屬性』]

* @return [type] [成功返回影響的行數,失敗返回false]

*/

function del($table,$where){

$sql = “DELETE FROM `{$table}` WHERE {$where}”;

$res = mysql_query($sql,$this-conn);

if($res mysql_affected_rows()){

return mysql_affected_rows();

}else{

return false;

}

}

}

?

使用案例:

?php

//包含資料庫操作類文件

include ‘mysql.class.php’;

//設置傳入參數

$hostname=’localhost’;

$username=’root’;

$password=’123456′;

$dbname=’aisi’;

$charset = ‘utf8’;

//實例化對象

$db = new Mysql($hostname,$username,$password,$dbname);

//獲取一條數據

$sql = “SELECT count(as_article_id) as count FROM as_article where as_article_type_id=1”;

$count = $db-getOne($sql);

//獲取多條數據

$sql = “SELECT * FROM as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit”;

$service = $db-getAll($sql);

//插入數據

$arr = array(

‘as_article_title’=’資料庫操作類’,

‘as_article_author’=’rex’,

);

$res = $db-insert(‘as_article’,$arr);

//更新數據

$arr = array(

‘as_article_title’=’實例化對象’,

‘as_article_author’=’Lee’,

);

$where = “as_article_id=1”;

$res = $db-update(‘as_article’,$arr,$where);

//刪除數據

$where = “as_article_id=1”;

$res = $db-del(‘as_article’,$where);

?

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/206350.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-07 17:50
下一篇 2024-12-07 17:50

相關推薦

  • 如何修改mysql的埠號

    本文將介紹如何修改mysql的埠號,方便開發者根據實際需求配置對應埠號。 一、為什麼需要修改mysql埠號 默認情況下,mysql使用的埠號是3306。在某些情況下,我們需…

    編程 2025-04-29
  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • Python棧操作用法介紹

    如果你是一位Python開發工程師,那麼你必須掌握Python中的棧操作。在Python中,棧是一個容器,提供後進先出(LIFO)的原則。這篇文章將通過多個方面詳細地闡述Pytho…

    編程 2025-04-29
  • Python操作數組

    本文將從多個方面詳細介紹如何使用Python操作5個數組成的列表。 一、數組的定義 數組是一種用於存儲相同類型數據的數據結構。Python中的數組是通過列表來實現的,列表中可以存放…

    編程 2025-04-29
  • 機智雲gagent屬於哪個協議?

    機智雲gagent主要是基於MQTT協議,同時支持TCP、TLS、WebSocket等多種協議。 一、MQTT協議介紹 MQTT全稱Message Queuing Telemetr…

    編程 2025-04-29
  • 使用Netzob進行網路協議分析

    Netzob是一款開源的網路協議分析工具。它提供了一套完整的協議分析框架,可以支持多種數據格式的解析和可視化,方便用戶對協議數據進行分析和定製。本文將從多個方面對Netzob進行詳…

    編程 2025-04-29
  • Python操作MySQL

    本文將從以下幾個方面對Python操作MySQL進行詳細闡述: 一、連接MySQL資料庫 在使用Python操作MySQL之前,我們需要先連接MySQL資料庫。在Python中,我…

    編程 2025-04-29
  • Python磁碟操作全方位解析

    本篇文章將從多個方面對Python磁碟操作進行詳細闡述,包括文件讀寫、文件夾創建、刪除、文件搜索與遍歷、文件重命名、移動、複製、文件許可權修改等常用操作。 一、文件讀寫操作 文件讀寫…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • Python代碼實現迴文數最少操作次數

    本文將介紹如何使用Python解決一道經典的迴文數問題:給定一個數n,按照一定規則對它進行若干次操作,使得n成為迴文數,求最少的操作次數。 一、問題分析 首先,我們需要了解迴文數的…

    編程 2025-04-29

發表回復

登錄後才能評論