<?php
Model class base class
Class model{
protected $db; Database Connection Object
protected $table; Table name
Protected $fields = Array (); Field List
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;
Calling the GetFields field
$this->getfields ();
}
/**
* Get table field List
*
*/
Private Function GetFields () {
$sql = "DESC". $this->table;
$result = $this->db->getall ($sql);
foreach ($result as $v) {
$this->fields[] = $v [' Field '];
if ($v [' Key '] = = ' PRI ') {
If there is a primary key, save it in the variable $pk
$PK = $v [' Field '];
}
}
If a primary key exists, it is added to the field list fields
if (Isset ($PK)) {
$this->fields[' pk '] = $PK;
}
}
/**
* Automatically insert records
* @access Public
* @param $list Array associative arrays
* @return Mixed successfully Returns the inserted ID, failure returns false
*/
Public Function Insert ($list) {
$field _list = "; Field List string
$value _list = "; Value List string
foreach ($list as $k = = $v) {
if (In_array ($k, $this->fields)) {
$field _list. = "'". $k. " `" . ‘,‘;
$value _list. = "'". $v. "'". ‘,‘;
}
}
Remove the comma on the right
$field _list = RTrim ($field _list, ', ');
$value _list = RTrim ($value _list, ', ');
Constructing SQL statements
$sql = "INSERT into ' {$this->table} ' ({$field _list}) VALUES ($value _list)";
If ($this->db->query ($sql)) {
# Insert succeeds, returning the last inserted record ID
return $this->db->getinsertid ();
return true;
} else {
# Insert failed, return false
return false;
}
}
/**
* Automatically update records
* @access public
* @param $list array of associative arrays that need to be updated
* @return Mixed successfully Returns the number of rows affected, failure returns false
*/
Public Function Update ($list) {
$uplist = ';//Update list string
$where = 0;//update condition, default 0
foreach ($list as $k = $v) {
if (In_array ($k, $this->fields)) {
if ($k = = $this->fields[' pk ') {
# is the primary key column, construction condition
$where = "' $k ' = $v ";
} else {
# non-primary key column, constructs an update list
$uplist. = "' $k ' = ' $v '". ",";
}
}
}
//Remove Uplist to the right of
$uplist = RTrim ($uplist, ', ');
Construct SQL statement
$sql = "UPDATE ' {$this->table} ' SET {$uplist} WHERE {$where}";
if ($this->db->query ($sql)) {
# succeeds and determines the number of records affected
if ($rows = Mysql_affected_rows ()) {
# has an affected number of records
return $rows;
} else {
# no number of records affected, no update operation
return false;
}
} else {
# failed, returning false
return false;
}
}
/**
* Automatic deletion
* @access Public
* @param $pk mixed can be an integral type or an array
* @return Mixed successfully Returns the number of deleted records, and the failure returns false
*/
Public Function Delete ($PK) {
$where = 0; Condition string
Determine if the $PK is an array or a single value, and then construct the appropriate conditions
if (Is_array ($PK)) {
# array
$where = "' {$this->fields[' pk ']} ' in (". Implode (', ', $PK) ");
} else {
# Single Value
$where = "' {$this->fields[' pk ']} ' = $PK";
}
Constructing SQL statements
$sql = "DELETE from ' {$this->table} ' WHERE $where ';
if ($this->db->query ($sql)) {
# success, and determine the number of records affected
if ($rows = Mysql_affected_rows ()) {
# have an affected record
return $rows;
} else {
# no affected Records
return false;
}
} else {
# Failed to return false
return false;
}
}
/**
* Get information by primary key
* @param $pk int PRIMARY key value
* @return Array Single record
*/
Public Function SELECTBYPK ($PK) {
$sql = "Select * from ' {$this->table} ' where ' {$this->fields[' pk ']} ' = $PK";
return $this->db->getrow ($sql);
}
/**
* Get the total number of records
* @param string $where query criteria, such as "id=1"
* @return number of records returned by query
*/
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);
}
/**
* Paging For information
* @param $offset int offset
* @param $limit int to fetch the number of records each time
* @param $where string where condition, default is empty
*/
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