需要封装数据库操作类,先看看我们需要哪些准备。
1.我们要了解sql语句的结构
2.要知道什么是连贯操作
3.关于占位符的使用,目的是为了让连贯操作打乱顺续不影响sql语句的生成
下面是步骤,先封装需要用到的属性
Class Model
{
protected $host;//主机名
protected $user;//用户名
protected $password;//密码
protected $dbname;//数据库名
protected $charset;//字符集
protected $prefix;//数据表前缀
//数据库连接资源
protected $link;
//表名
protected $tableName;
//操作数组
protected $options;
然后写构造函数来初始化属性,这当中的$link调用一个connect()方法,$tableName调用getTableName方法,并调用initOptions方法将操作数组初始化。
//构造方法
public function __construct($config)
{
$this->host = $config['DB_HOST'];
$this->user = $config['DB_USER'];
$this->password = $config['DB_PWD'];
$this->dbname = $config['DB_NAME'];
$this->charset = $config['DB_CHARSET'];
$this->prefix = $config['DB_PREFIX'];
$this->link = $this->connect();
$this->tableName = $this->getTableName();
//初始化操作数组
$this->initOptions();
}
connect()方法
protected function connect()
{
//连接数据库
$link = mysqli_connect($this->host, $this->user, $this->password, $this->dbname);
if (!$link) {
die('数据库连接失败');
}
//设置字符集
mysqli_set_charset($link, $this->charset);
return $link;
}
getTableName()
protected function getTableName()
{
if (!empty($this->tableName)) {
$tableName = $this->prefix . $this->tableName;
} else {
$className = get_class($this);
$table = strtolower(substr($className, 0, -5));
$tableName = $this->prefix . $table;
}
return $tableName;
}
initOptions()方法
protected function initOptions()
{
$arr = ['where', 'table', 'field', 'group', 'order', 'having', 'limit'];
foreach ($arr as $value) {
$this->options[$value] = '';
}
if ($value == 'table') {
$this->options[$value] = $this->tableName;
}
}
然后是操作的函数,用来拼接sql语句。
//field
function field($field)
{
if (!empty($field)) {
if (is_string($field)) {
$this->options['field'] = $field;
} else if (is_array($field)) {
$this->options['field'] = join(',', $field);
}
}
return $this;
}
//table
function table($table)
{
if (!empty($table)) {
$this->options['table'] = $table;
}
return $this;
}
//where
function where($where)
{
if (!empty($where)) {
$this->options['where'] = 'where ' . $where;
}
return $this;
}
//group
function group($group)
{
if (!empty($group)) {
$this->options['group'] = 'group by' . $group;
}
return $this;
}
//having
function having($having)
{
if (!empty($having)) {
$this->options['having'] = 'having' . $having;
}
return $this;
}
//order
function order($order)
{
if (!empty($order)) {
$this->options['order'] = 'order by' . $order;
}
return $this;
}
//limit
function limit($limit)
{
if (!empty($limit)) {
if (is_string($limit)) {
$this->options['limit'] = 'limit ' . $limit;
} else if (is_array($limit)) {
$this->options['limit'] = 'limit ' . join(',', $limit);
}
return $this;
}
}
最后写上我们的增删改查的函数即可。
//select
function select()
{
$sql = 'select %FIELD% from %TABLE% %WHERE% %GROUP% %HAVING% %ORDER% %LIMIT%';
$sql = str_replace(['%FIELD%', '%TABLE%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%'],
[$this->options['field'], $this->options['table'], $this->options['where'], $this->options['group'], $this->options['having'], $this->options['order'], $this->options['limit']], $sql);
$this->sql = $sql;
//执行sql
return $this->query($sql);
}
//query
function query($sql)
{ $this->initOptions();
/*var_dump($sql);
die();*/
$result = mysqli_query($this->link, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$newRow[] = $row;
}
return $newRow;
}
function insert($data)
{
//读数组进行处理
$data = $this->changeData($data);
$keys = array_keys($data);
$values = array_values($data);
$sql = 'insert into %TABLE% (%FIELD%) VALUES (%VALUES%)';
$sql = str_replace(['%TABLE%', '%FIELD%', '%VALUES%'], [$this->options['table'], join(',', $keys), join(',', $values)], $sql);
$this->sql = $sql;
return $this->exec($sql, true);
}
function delete()
{
$sql = 'delete from %TABLE% %WHERE%';
$sql = str_replace(['%TABLE%', '%WHERE%'],
[$this->options['table'], $this->options['where']], $sql);
$this->sql = $sql;
return $this->exec($sql,false);
}
function update($data){
$data=$this->changeData($data);
$value=$this->changeUpdate($data);
$sql = 'update %TABLE% set %VALUE% %WHERE%';
$sql = str_replace(['%TABLE%','%VALUE%', '%WHERE%'],
[$this->options['table'],$value, $this->options['where']], $sql);
$this->sql = $sql;
return $this->exec($sql,false);
}
//exec
function exec($sql, $isInsert)
{ $this->initOptions();
$result = mysqli_query($this->link, $sql);
if ($isInsert) {
return mysqli_insert_id($this->link);
} else {
return mysqli_affected_rows($this->link);
}
}
//更新函数数据源格式
function changeUpdate($data){
foreach($data as $key=>$value){
$newdata[]=$key.'='.$value;
}return join(',',$newdata);
}
//添加数据数据源更改
function changeData($data)
{
foreach ($data as $key => $value) {
if (is_string($value)) {
$value = '"' . $value . '"';
}
$newData[$key] = $value;
}
return $newData;
}
function __get($name)
{
if ($name == 'sql') {
return $this->sql;
}
return false;
}