標籤:int ble off iss page insert field host turn
<?php
//模型類基類
class Model{
protected $db; //資料庫連接對象
protected $table; //表名
protected $fields = array(); //欄位列表
public function __construct($table){
$dbconfig[‘host‘] = $GLOBALS[‘config‘][‘host‘];
$dbconfig[‘user‘] = $GLOBALS[‘config‘][‘user‘];
$dbconfig[‘password‘] = $GLOBALS[‘config‘][‘password‘];
$dbconfig[‘dbname‘] = $GLOBALS[‘config‘][‘dbname‘];
$dbconfig[‘port‘] = $GLOBALS[‘config‘][‘port‘];
$dbconfig[‘charset‘] = $GLOBALS[‘config‘][‘charset‘];
$this->db = new Mysql($dbconfig);
$this->table = $GLOBALS[‘config‘][‘prefix‘] . $table;
//調用getFields欄位
$this->getFields();
}
/**
* 擷取表欄位列表
*
*/
private function getFields(){
$sql = "DESC ". $this->table;
$result = $this->db->getAll($sql);
foreach ($result as $v) {
$this->fields[] = $v[‘Field‘];
if ($v[‘Key‘] == ‘PRI‘) {
//如果存在主鍵的話,則將其儲存到變數$pk中
$pk = $v[‘Field‘];
}
}
//如果存在主鍵,則將其加入到欄位列表fields中
if (isset($pk)) {
$this->fields[‘pk‘] = $pk;
}
}
/**
* 自動插入記錄
* @access public
* @param $list array 關聯陣列
* @return mixed 成功返回插入的id,失敗則返回false
*/
public function insert($list){
$field_list = ‘‘; //欄位列表字串
$value_list = ‘‘; //值列表字串
foreach ($list as $k => $v) {
if (in_array($k, $this->fields)) {
$field_list .= "`".$k."`" . ‘,‘;
$value_list .= "‘".$v."‘" . ‘,‘;
}
}
//去除右邊的逗號
$field_list = rtrim($field_list,‘,‘);
$value_list = rtrim($value_list,‘,‘);
//構造sql語句
$sql = "INSERT INTO `{$this->table}` ({$field_list}) VALUES ($value_list)";
if ($this->db->query($sql)) {
# 插入成功,返回最後插入的記錄id
return $this->db->getInsertId();
//return true;
} else {
# 插入失敗,返回false
return false;
}
}
/**
* 自動更新記錄
* @access public
* @param $list array 需要更新的關聯陣列
* @return mixed 成功返回受影響的記錄行數,失敗返回false
*/
public function update($list){
$uplist = ‘‘; //更新列表字串
$where = 0; //更新條件,預設為0
foreach ($list as $k => $v) {
if (in_array($k, $this->fields)) {
if ($k == $this->fields[‘pk‘]) {
# 是主鍵列,構造條件
$where = "`$k`=$v";
} else {
# 非主鍵列,構造更新列表
$uplist .= "`$k`=‘$v‘".",";
}
}
}
//去除uplist右邊的
$uplist = rtrim($uplist,‘,‘);
//構造sql語句
$sql = "UPDATE `{$this->table}` SET {$uplist} WHERE {$where}";
if ($this->db->query($sql)) {
# 成功,並判斷受影響的記錄數
if ($rows = mysql_affected_rows()) {
# 有受影響的記錄數
return $rows;
} else {
# 沒有受影響的記錄數,沒有更新操作
return false;
}
} else {
# 失敗,返回false
return false;
}
}
/**
* 自動刪除
* @access public
* @param $pk mixed 可以為一個整型,也可以為數組
* @return mixed 成功返回刪除的記錄數,失敗則返回false
*/
public function delete($pk){
$where = 0; //條件字串
//判斷$pk是數組還是單值,然後構造相應的條件
if (is_array($pk)) {
# 數組
$where = "`{$this->fields[‘pk‘]}` in (".implode(‘,‘, $pk).")";
} else {
# 單值
$where = "`{$this->fields[‘pk‘]}`=$pk";
}
//構造sql語句
$sql = "DELETE FROM `{$this->table}` WHERE $where";
if ($this->db->query($sql)) {
# 成功,並判斷受影響的記錄數
if ($rows = mysql_affected_rows()) {
# 有受影響的記錄
return $rows;
} else {
# 沒有受影響的記錄
return false;
}
} else {
# 失敗返回false
return false;
}
}
/**
* 通過主鍵擷取資訊
* @param $pk int 主索引值
* @return array 單條記錄
*/
public function selectByPk($pk){
$sql = "select * from `{$this->table}` where `{$this->fields[‘pk‘]}`=$pk";
return $this->db->getRow($sql);
}
/**
* 擷取總的記錄數
* @param string $where 查詢條件,如"id=1"
* @return number 返回查詢的記錄數
*/
public function total($where){
if(empty($where)){
$sql = "select count(*) from {$this->table}";
}else{
$sql = "select count(*) from {$this->table} where $where";
}
return $this->db->getOne($sql);
}
/**
* 分頁擷取資訊
* @param $offset int 位移量
* @param $limit int 每次取記錄的條數
* @param $where string where條件,預設為空白
*/
public function pageRows($offset, $limit,$where = ‘‘){
if (empty($where)){
$sql = "select * from {$this->table} limit $offset, $limit";
} else {
$sql = "select * from {$this->table} where $where limit $offset, $limit";
}
return $this->db->getAll($sql);
}
}
mvc-Model.class.php