<?php
require './lib/DbClass.php';
class MysqliCurd{
public $_db;//数据库
public $_sql;//sql语句
public function __construct()
{
$this->_db = Db::getDB();
}
/**
* 数据库添加操作
* @param string $tName 表名
* @param array $field 字段数组
* @param array $val 值数组
* @param bool $is_lastInsertId 是否返回添加ID
* @return int 默认返回成功与否,$is_lastInsertId 为true,返回添加ID
*/
public function insert($tName, $fields, $vals, $is_lastInsertId=FALSE)
{
try {
if (!is_array($fields) || !is_array($vals))
exit($this->getError(__FUNCTION__, __LINE__));
$fields = $this->formatArr($fields);
$vals = $this->formatArr($vals, false);
$tName = $this->formatTabName($tName);
$this->_sql = "INSERT INTO {$tName} ({$fields}) VALUES ({$vals})";
//echo $this->_sql;die;
if (!$is_lastInsertId) {
$row = $this->_db->query($this->_sql);
//关闭连接
$this->_db->close();
return $row;
} else {
$this->_db->query($this->_sql);
$lastId = (int)($this->_db)->insert_id;
//关闭连接
$this->_db->close();
return $lastId;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* 返回单个字段数据或单条记录
* @param string $tName 表名
* @param string $condition 条件
* @param string or array $fields 返回的字段,默认是*
* @return string || array
*/
public function getRow($tName, $condition='', $fields="*") {
try {
if (!is_string($tName) || !is_string($condition)
|| !is_string($fields) || empty($fields))
exit($this->getError(__FUNCTION__, __LINE__));
//$tName = $this->formatTabName($tName);
$this->_sql = "SELECT {$fields} FROM {$tName} ";
$this->_sql .= ($condition=='' ? '' : "WHERE {$condition}") . " LIMIT 1";
//echo $this->_sql;die;
$sth = $this->_db->query($this->_sql);
//如果返回0返回空数组
if(empty($sth->num_rows))
{
return [];
}
if($sth->num_rows > 0)
{
//返回关联数组
$result = $sth->fetch_assoc();
$sth->free();//释放结果集
$this->_db->close();//关闭连接
return $result;//返回受影响的行数
}
} catch (PDOException $e) {
exit($e->getMessage());
//return false;
}
}
/**
* 返回多条记录
* @param string $tName 表名
* @param string or array $fields 返回的字段,默认是*
* @return string || array
*/
public function getAll($tName,$fields="*") {
try {
if (!is_string($tName) || !is_string($fields) || empty($fields))
exit($this->getError(__FUNCTION__, __LINE__));
//$tName = $this->formatTabName($tName);
$this->_sql = "SELECT {$fields} FROM {$tName} ";
//echo $this->_sql;die;
$sth = $this->_db->query($this->_sql);
//如果返回0返回空数组
if(empty($sth->num_rows))
{
//关闭连接
$this->_db->close();
return [];
}
if($sth->num_rows > 0)
{
//返回关联数组结果集
$res = [];
$i = 0;
while ($i < $sth->num_rows)
{
$res[] = $sth->fetch_assoc();
$i++;
}
$sth->free();//释放结果集
//关闭连接
$this->_db->close();
return $res;//返回数据
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* 数据库修改操作
* @param string $tName 表名
* @param array $field 字段数组
* @param array $fieldVal 值数组
* @param string $condition 条件
* @return int 受影响的行数
*/
public function update($tName, $fieldVal, $condition) {
try {
if (!is_array($fieldVal) || !is_string($tName) || !is_string($condition))
exit($this->getError(__FUNCTION__, __LINE__));
$tName = $this->formatTabName($tName);
$upStr = '';
foreach ($fieldVal as $k=>$v) {
$upStr .= '`'.$k . '`=' . '\'' . $v . '\'' . ',';
}
$upStr = rtrim($upStr, ',');
$this->_sql = "UPDATE {$tName} SET {$upStr} WHERE {$condition}";
//echo $this->_sql;die;
$row = $this->_db->query($this->_sql);
$this->_db->close();
return $row;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* 数据库删除操作(注:必须添加 where 条件)
* @param string $tName 表名
* @param string $condition 条件
* @return int 受影响的行数
*/
public function del($tName, $condition) {
try {
if (!is_string($tName) || !is_string($condition))
exit($this->getError(__FUNCTION__, __LINE__));
$tName= $this->formatTabName($tName);
$this->_sql = "DELETE FROM {$tName} WHERE {$condition}";
//echo $this->_sql;die;
$row = $this->_db->query($this->_sql);
//关闭连接
$this->_db->close();
return $row;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* 数据库删除多条数据
* @param string $tName 表名
* @param string $field 依赖字段
* @param array $ids 删除数组
* @return int 受影响的行数
*/
public function delMulti($tName, $field, $ids) {
try {
if (!is_string($tName) || !is_array($ids))
exit($this->getError(__FUNCTION__, __LINE__));
$delStr = '';
$tName = $this->formatTabName($tName);
$field = $this->formatTabName($field);
foreach ($ids as $v) {
$delStr .= $v . ',';
}
$delStr = rtrim($delStr, ',');
$this->_sql = "DELETE FROM {$tName} WHERE {$field} IN ({$delStr})";
//echo $this->_sql;die;
$row = $this->_db->query($this->_sql);
//关闭连接
$this->_db->close();
return $row;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* 获取表格的最后一个主键(注:针对 INT 类型)
* @param string $tName 表名
* @return int
*/
public function insertId($tName) {
try {
if (!is_string($tName))
exit($this->getError(__FUNCTION__, __LINE__));
$this->_sql = "SHOW TABLE STATUS LIKE '{$tName}'";
$result = $this->_db->query($this->_sql);
//关闭连接
$this->_db->close();
$insert_id = 0;
foreach ($result as $v) {
$insert_id = $v['Auto_increment'];
}
return (int)$insert_id;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* 检查数据是否已经存在(依赖条件)
* @param string $tName 表名
* @param string $field 依赖的字段
* @return bool
*/
public function exists($tName, $condition) {
try {
if (!is_string($tName) || !is_string($condition))
exit($this->getError(__FUNCTION__, __LINE__));
$tName = $this->formatTabName($tName);
$this->_sql = "SELECT COUNT(*) AS total FROM {$tName} WHERE {$condition}";
$result = $this->_db->query($this->_sql);
//关闭连接
$this->_db->close();
foreach ($result as $v) {
$b = $v['total'];
}
if ($b) {
return true;
} else {
return false;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* 检查数据是否已经存在(依赖 INT 主键)
* @param string $tName 表名
* @param string $primary 主键
* @param int $id 主键值
* @return bool
*/
public function existsByPK($tName, $primary, $id) {
try {
if (!is_string($tName) || !is_string($primary)
|| !is_int($i
评论0