PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫(kù)連接工具類【定義與用法】
本文實(shí)例講述了PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫(kù)連接工具類。分享給大家供大家參考,具體如下:
mysql.class.php:
<?php
class mysql
{
private $mysqli;
private $result;
/**
* 數(shù)據(jù)庫(kù)連接
* @param $config 配置數(shù)組
*/
public function connect($config)
{
$host = $config['host']; //主機(jī)地址
$username = $config['username'];//用戶名
$password = $config['password'];//密碼
$database = $config['database'];//數(shù)據(jù)庫(kù)
$port = $config['port']; //端口號(hào)
$this->mysqli = new mysqli($host, $username, $password, $database, $port);
}
/**
* 數(shù)據(jù)查詢
* @param $table 數(shù)據(jù)表
* @param null $field 字段
* @param null $where 條件
* @return mixed 查詢結(jié)果數(shù)目
*/
public function select($table, $field = null, $where = null)
{
$sql = "SELECT * FROM {$table}";
if (!empty($field)) {
$field = '`' . implode('`,`', $field) . '`';
$sql = str_replace('*', $field, $sql);
}
if (!empty($where)) {
$sql = $sql . ' WHERE ' . $where;
}
$this->result = $this->mysqli->query($sql);
return $this->result->num_rows;
}
/**
* @return mixed 獲取全部結(jié)果
*/
public function fetchAll()
{
return $this->result->fetch_all(MYSQLI_ASSOC);
}
/**
* 插入數(shù)據(jù)
* @param $table 數(shù)據(jù)表
* @param $data 數(shù)據(jù)數(shù)組
* @return mixed 插入ID
*/
public function insert($table, $data)
{
foreach ($data as $key => $value) {
$data[$key] = $this->mysqli->real_escape_string($value);
}
$keys = '`' . implode('`,`', array_keys($data)) . '`';
$values = '\'' . implode("','", array_values($data)) . '\'';
$sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )";
$this->mysqli->query($sql);
return $this->mysqli->insert_id;
}
/**
* 更新數(shù)據(jù)
* @param $table 數(shù)據(jù)表
* @param $data 數(shù)據(jù)數(shù)組
* @param $where 過(guò)濾條件
* @return mixed 受影響記錄
*/
public function update($table, $data, $where)
{
foreach ($data as $key => $value) {
$data[$key] = $this->mysqli->real_escape_string($value);
}
$sets = array();
foreach ($data as $key => $value) {
$kstr = '`' . $key . '`';
$vstr = '\'' . $value . '\'';
array_push($sets, $kstr . '=' . $vstr);
}
$kav = implode(',', $sets);
$sql = "UPDATE {$table} SET {$kav} WHERE {$where}";
$this->mysqli->query($sql);
return $this->mysqli->affected_rows;
}
/**
* 刪除數(shù)據(jù)
* @param $table 數(shù)據(jù)表
* @param $where 過(guò)濾條件
* @return mixed 受影響記錄
*/
public function delete($table, $where)
{
$sql = "DELETE FROM {$table} WHERE {$where}";
$this->mysqli->query($sql);
return $this->mysqli->affected_rows;
}
}
使用方法
<?php
require_once 'mysql.class.php';
/* 配置連接參數(shù) */
$config = array(
'type' => 'mysql',
'host' => 'localhost',
'username' => 'woider',
'password' => '3243',
'database' => 'php',
'port' => '3306'
);
/* 連接數(shù)據(jù)庫(kù) */
$mysql = new mysql();
$mysql->connect($config);
/* 查詢數(shù)據(jù) */
//1、查詢所有數(shù)據(jù)
$table = 'mysqli';//數(shù)據(jù)表
$num = $mysql->select($table);
echo '共查詢到' . $num . '條數(shù)據(jù)';
print_r($mysql->fetchAll());
//2、查詢部分?jǐn)?shù)據(jù)
$field = array('username', 'password'); //過(guò)濾字段
$where = 'id % 2 =0'; //過(guò)濾條件
$mysql->select($table, $field, $where);
print_r($mysql->fetchAll());
/* 插入數(shù)據(jù) */
$table = 'mysqli';//數(shù)據(jù)表
$data = array( //數(shù)據(jù)數(shù)組
'username' => 'admin',
'password' => sha1('admin')
);
$id = $mysql->insert($table, $data);
echo '插入記錄的ID為' . $id;
/* 修改數(shù)據(jù) */
$table = 'mysqli';//數(shù)據(jù)表
$data = array(
'password' => sha1('nimda')
);
$where = 'id = 44';
$rows = $mysql->update($table, $data, $where);
echo '受影響的記錄數(shù)量為' . $rows . '條';
/* 刪除數(shù)據(jù) */
$table = 'mysqli';
$where = 'id = 45';
$rows = $mysql->delete($table, $where);
echo '已刪除' . $rows . '條數(shù)據(jù)';
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫(kù)操作類與用法示例
- PHP封裝的mysqli數(shù)據(jù)庫(kù)操作類示例
- php基于PDO實(shí)現(xiàn)功能強(qiáng)大的MYSQL封裝類實(shí)例
- php基于單例模式封裝mysql類完整實(shí)例
- php封裝的mysqli類完整實(shí)例
- php mysql 封裝類實(shí)例代碼
- php封裝的連接Mysql類及用法分析
- php實(shí)現(xiàn)mysql封裝類示例
- PHP訪問(wèn)MYSQL數(shù)據(jù)庫(kù)封裝類(附函數(shù)說(shuō)明)
- php鏈?zhǔn)讲僮鱩ysql數(shù)據(jù)庫(kù)(封裝類帶使用示例)
相關(guān)文章
詳談PHP面向?qū)ο笾谐S玫年P(guān)鍵字和魔術(shù)方法
下面小編就為大家?guī)?lái)一篇詳談PHP面向?qū)ο笾谐S玫年P(guān)鍵字和魔術(shù)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
PHP.ini中配置屏蔽錯(cuò)誤信息顯示和保存錯(cuò)誤日志的例子
這篇文章主要介紹了PHP.ini中配置屏蔽錯(cuò)誤信息顯示和保存錯(cuò)誤日志的例子,需要的朋友可以參考下2014-05-05
php中強(qiáng)制下載文件的代碼(解決了IE下中文文件名亂碼問(wèn)題)
以下這段代碼作用是:瀏覽器提交excel格式的數(shù)據(jù)和文件名到服務(wù)器上,PHP將請(qǐng)求轉(zhuǎn)化為可下載的excel文件,并要求瀏覽器彈出文件下載提示窗口2011-05-05
php往mysql中批量插入數(shù)據(jù)實(shí)例教程
這篇文章主要給大家介紹了關(guān)于php往mysql中批量插入數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-12-12
php字符串操作常見(jiàn)問(wèn)題小結(jié)
這篇文章主要介紹了php字符串操作常見(jiàn)問(wèn)題,結(jié)合實(shí)例形式分析了php針對(duì)json的操作及字符串轉(zhuǎn)換問(wèn)題,需要的朋友可以參考下2016-10-10
linux下使用crontab實(shí)現(xiàn)定時(shí)PHP計(jì)劃任務(wù)失敗的原因分析
這篇文章主要介紹了linux下使用crontab實(shí)現(xiàn)定時(shí)PHP計(jì)劃任務(wù)失敗的原因分析,需要的朋友可以參考下2014-07-07

