## :-: **【注】:此文檔不兼容成品搭建系統v1.x 版本**
# **MySql 基礎使用**
> 具體使用方法詳見 cmspro/lib/mysql.lib.php
## 系統內置數據庫操作
**附加可選參數**
```
getField 指定欲取的字段,多個請以(,)分號隔開//例如 ->getField('uid','tid')
where 查詢條件 //例如 ->where("id=100")
order 指定排序 //例如 ->order('id asc') ID正序 | ->order('id desc') ID倒序
limit 指定欲取的數據條數 //例如 ->limit(5) 獲取5條數據 | ->limit(100,10) 從第100條開始獲取10條數據
field 指定欲取的字段 名稱,多個請以(,)分號隔開 //例如 ->field('uid','tid')
```
**添加數據**
單條數據添加
```
/**
* 添加數據
*/
$data = array(
'port' => $port,
'type' => 'port',
'ps' => $ps,
'addtime' => date('Y-m-d H:i:s')
);
DB('table')->add($data);
```
多條數據添加
```
/**
* 添加數據
*/
$c = 10; //添加10條數據
for ($i=1;$c>=$i;$i++) {
$data[] = array(
'port' => $port,
'type' => 'port',
'ps' => $ps,
'addtime' => date('Y-m-d H:i:s')
);
}
DB('table')->adds($data);
```
**更新數據**
```
/**
* 更新數據,返回受影響行
*/
//更新全部
DB('table')->save($data);
//條件更新
DB('table')->where("id='100'")->save($data);
```
**指定字段加**
```
/**
* 指定字段加N,默認加1
*/
//全部修改
DB('table')->sum('number', '100');
//條件修改
DB('table')->where("id='100'")->sum('number', '100');
```
**刪除數據**
```
/**
* 刪除數據(返回受影響行)
*/
//全部修改
DB('table')->delete();
//條件修改
DB('table')->where("id='100'")->delete();
```
**取數據集**
```
/**
* 取數據集(返回符合條件的所有數據)
*
* 可選擇參數 field 指定欲取的字段,多個請以(,)分號隔開
* 可選參數 where 條件查詢
* 可選參數 order 指定 字段 排序 desc.倒序|asc.正序
* 可選參數 limit 指定欲取的數據條數(1):一條 |(30,60):從第30條到第60條
*/
//例1
$rows = DB('table')->field('id,name,username,password,accept')->where("type='1'")->select();
//例2
$rows = DB('table')->where("type='1'")->select();
foreach ($rows as $res) {
echo $res['id'];
}
```
**取一行數據**
```
/**
* 取一行數據(只返回第一條數據)
*
* 可選擇參數 field 指定欲取的字段,多個請以(,)分號隔開
* 可選參數 where 條件查詢
* 可選參數 order 指定排序 desc.倒序|asc.正序
*/
$row = DB('table')->field('id,name,username,password,accept')->where("type='1'")->find();
echo $row['id'];
```
**取指定字段值**
```
/**
* 取指定字段值(當只有一條數據時,直接返回鍵值,否則返回數組,失敗返回false)
*/
DB('table')->where("id=1")->getField('status');
```
**設定指定字段值**
```
/**
* 設定指定字段值
*/
DB('table')->where("id=1")->setField('status', '1');
```
**取符合條件的數據總行數**
```
/**
* 取符合條件的數據總行數
* 可選參數 where 條件查詢
*/
//全部表
DB('table')->getCount();
//條件查詢
DB('table')->where("status=1")->getCount();
```
**執行SQL語句,返回受影響行**
```
/**
* 執行SQL語句,返回受影響行
*
* $sql SQL語句
* $data 更新內容
*/
//例1
$sqlstr = "INSERT INTO logs(type,log,addtime) VALUES(:type,:log,:addtime)";
DB('table')->execute($sqlstr, $data);
//例2
$sql = 'alter table tasks add column param text;';
DB('table')->execute($sql, null);
```
**同表復制數據**
返回創建的ID
```
/**
* 同表復制數據
* @param String $id 對應ID
* @param String $data 所復制的數據
* @param String $type 1:默認不復制ID 其它值復制數據中的ID
* @return $this
*/
//查詢信息
$row = DB('try')->where("id={$_GET['id']}")->find();
//自定義數據(可選)
$row['status'] = '-1';
$row['lasttime'] = time();
$row['addtime'] = time();
$row['starttime'] = time();
$row['endtime'] = time() + 86400 * CP_TRY_OUTTIME;
//復制信息
$tid = DB('try')->copy("id={$_GET['id']}", $row);
```
**執行SQL語句,返回數據集**
```
/**
* 執行SQL語句,返回數據集
*
* 純原生SQL執行
* 限查詢
*/
//例1
$sqlstr = "INSERT INTO logs(type,log,addtime) VALUES(:type,:log,:addtime)";
$rows1 = DB('table')->query($sqlstr, $data);
foreach ($rows1 as $res) {
echo $res['id'];
}
//例2
$sql = 'alter table tasks add column param text;';
$rows2 = DB('table')->query($sql, null);
foreach ($rows2 as $res) {
echo $res['id'];
}
```
**純原生SQL執行**
```
/*
* 純原生SQL執行
* 限插入或更新
*/
//例1
$sqlstr = "INSERT INTO logs(type,log,addtime) VALUES(:type,:log,:addtime)";
$rows1 = DB('table')->execute($sqlstr, $data);
foreach ($rows1 as $res) {
echo $res['id'];
}
//例2
$sql = 'alter table tasks add column param text;';
$rows2 = DB('table')->execute($sql, null);
foreach ($rows2 as $res) {
echo $res['id'];
}
```
## 自定義鏈接數據庫操作
**載入數據庫操作類**
```
//載入數據庫操作類
cp::re('mysql');
//配置數據庫信息
$conn['DB_HOST'] = '127.0.0.1'; //主機地址 本地一般采用127.0.0.1或localhost
$conn['DB_PORT'] = '3306'; //數據庫端口
$conn['DB_USER'] = 'root'; //數據帳戶
$conn['DB_PWD'] = '123456'; //數據密碼
$conn['DB_NAME'] = 'test'; //數據庫名
$conn['DB_PREFIX'] = 'cp_'; //表前綴
$conn['DB_CHARSET'] = 'utf8'; //數據庫編碼
$conn['DB_TABLE'] = 'table'; //需要操作的數據庫表
$db = Mysql::start($conn);
//數據庫操作 其它操作參考【系統內置數據庫操作】
$db->add($data);
```
**自定義鏈接數據庫封裝操作**
```
function M($table) {
cp::re('mysql');
$conn['DB_HOST'] = '127.0.0.1'; //主機地址 本地一般采用127.0.0.1或localhost
$conn['DB_PORT'] = '3306'; //數據庫端口
$conn['DB_USER'] = 'root'; //數據帳戶
$conn['DB_PWD'] = '123456'; //數據密碼
$conn['DB_NAME'] = 'test'; //數據庫名
$conn['DB_PREFIX'] = 'cp_'; //表前綴
$conn['DB_CHARSET'] = 'utf8'; //數據庫編碼
$conn['DB_TABLE'] = $table; //需要操作的數據庫表
return Mysql::start($conn);
}
//數據庫操作 其它操作參考【系統內置數據庫操作】
M('table')->add($data);
```
**延申學習**
[讀取數據進行分頁獲取內容](讀取數據進行分頁.md)