日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

php连接数据库封装函数,PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】...

發(fā)布時間:2024/10/5 数据库 147 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php连接数据库封装函数,PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例講述了PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類。分享給大家供大家參考,具體如下:

mysql.class.php:

class mysql

{

private $mysqli;

private $result;

/**

* 數(shù)據(jù)庫連接

* @param $config 配置數(shù)組

*/

public function connect($config)

{

$host = $config['host']; //主機地址

$username = $config['username'];//用戶名

$password = $config['password'];//密碼

$database = $config['database'];//數(shù)據(jù)庫

$port = $config['port']; //端口號

$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 過濾條件

* @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 過濾條件

* @return mixed 受影響記錄

*/

public function delete($table, $where)

{

$sql = "DELETE FROM {$table} WHERE {$where}";

$this->mysqli->query($sql);

return $this->mysqli->affected_rows;

}

}

使用方法

require_once 'mysql.class.php';

/* 配置連接參數(shù) */

$config = array(

'type' => 'mysql',

'host' => 'localhost',

'username' => 'woider',

'password' => '3243',

'database' => 'php',

'port' => '3306'

);

/* 連接數(shù)據(jù)庫 */

$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、查詢部分數(shù)據(jù)

$field = array('username', 'password'); //過濾字段

$where = 'id % 2 =0'; //過濾條件

$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ù)';

希望本文所述對大家PHP程序設(shè)計有所幫助。

總結(jié)

以上是生活随笔為你收集整理的php连接数据库封装函数,PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。