PHP基础MySQL方法
话不多说,直接上代码
<?php
/**
* 数据库连接方法
* @param string $host 主机名
* @param string $user 用户名
* @param string $pwd 密码
* @param string $table 库名
* @param string $charset 设置字符集
* @return $link 返回$link
*/
function connect($host , $user , $pwd , $table , $charset)
{
$link = mysqli_connect($host , $user , $pwd);
if (!$link) {
exit('数据库连接失败,请重新连接');
}
mysqli_set_charset($link , $charset);
mysqli_select_db($link , $table);
return $link;
}
/**
* 查询数据表中的所有字段
* @param object $link 数据库连接
* @param string $tableName 表名
* @return $data 返回表中的所有字段(Array)
*/
function desc($link , $tableName)
{
$sql = "DESC $tableName";
$res = mysqli_query($link , $sql);
if ($res && mysqli_affected_rows($link)) {
while ($rows = mysqli_fetch_assoc($res)) {
// var_dump($rows);
$data[] = $rows['Field'];
}
// var_dump($data);
return $data;
}
}
/**
* 数据库查询语句
* @param object $link 数据库连接
* @param string $fields 字段
* @param string $tableName 表名
* @param string $where 条件
* @param string $orderBy 排序
* @param string $limit 偏移量
* @return $data 返回查询数据
*/
function select($link , $fields , $tableName , $where=null , $orderBy=null , $limit=null)
{
//select [all | distinct] 字段或表达式列表 [from子句] [where子句] [group by子句] [having子句] [order by子句] [limit子句];
if (empty($fields)) {
$fields = '*';
}else{
$data = desc($link , $tableName);
// var_dump($data);
$aField = explode(',', $fields);
// var_dump($aField);
foreach ($aField as $key => $value) {
$val[] = trim($value);
}
// var_dump($val);
if ($val == '*') {
$fields = '*';
}else{
$intersection = array_intersect($val, $data);
$sField = implode(',', $intersection);
// echo $sField;
}
}
if (empty($where)) {
$where = null;
}else{
$where = 'WHERE '.$where;
}
if (empty($orderBy)) {
$orderBy = null;
}else{
$orderBy = 'ORDER BY '.$orderBy;
}
if (empty($limit)) {
$limit = null;
}else{
$limit = $limit;
}
$sql = "SELECT $fields FROM $tableName $where $orderBy $limit";
// echo $sql;
$res = mysqli_query($link , $sql);
if ($res && mysqli_affected_rows($link)) {
while ($rows = mysqli_fetch_assoc($res)) {
$newFields[] = $rows;
}
return $newFields;
}else{
return false;
}
}
/**
* 数据添加
* @param object $link 数据库连接
* @param string $tableName 表名
* @param array $arr 以数组的形式插入数据
* @return 返回主键自增id或者false
*/
function insert($link , $tableName , $arr)
{
$keys = array_keys($arr);
// var_dump($keys);
$values = array_values($arr);
// var_dump($values);
$key = implode(',', $keys);
// echo $key;
$val = implode(',', praseVal($values));
// echo $val;
//insert [into] 表名 [(字段名1,字段名2,....)] values (值表达式1,值表达式2,....)
$sql = "INSERT INTO $tableName (".$key.') VALUES ('.$val.')';
// echo $sql;
$res = mysqli_query($link , $sql);
if ($res && mysqli_affected_rows($link)) {
return mysqli_insert_id($link);
}else{
return false;
}
}
/**
* 处理添加单引号
* @param array/string/null $data 数据
* @return $data 返回处理后的数据
*/
function praseVal($data)
{
if (is_array($data)) {
$data = array_map('praseVal', $data);
// var_dump($data);
} else if (is_string($data)) {
$data = "'".str_replace(",","','",$data)."'";
}else if (is_null($data)) {
$data = null;
}
return $data;
}
/**
* 删除方法
* @param object $link 数据库连接
* @param string $tableName 表名
* @param array/string $where 条件(id)
* @return 返回受影响行数
*/
function del($link , $tableName , $where)
{
//delete from 表名 [where条件] [order排序] [limit限定];
/*if (is_array($where)) {
$where = implode(',', $where);
$sql = "DELETE FROM $tableName WHERE id IN (".$where.')';
echo $sql;die;
}else{
$sql = "DELETE FROM $tableName WHERE id = ".$where;
echo $sql;die;
}*/
$sql = "DELETE FROM $tableName WHERE $where ";
// echo $sql;
$res = mysqli_query($link , $sql);
if ($res && mysqli_affected_rows($link)) {
return mysqli_affected_rows($link);
}else{
return false;
}
}
/**
* 修改数据
* @param object $link 连接数据库
* @param string $tableName 表名
* @param array $data 字段=值,字段2=值2..
* @param string $where 修改条件
* @return 返回受影响行数/false
*/
function update($link , $tableName , $data , $where)
{
//update 表名 set 字段名1=值表达式1,字段名2=值表达式2,....[where条件] [order排序] [limit限定];
// var_dump($data);
$val = implode(',', praseData($data));
// var_dump($val);
// die;
$sql = "UPDATE $tableName SET $val WHERE $where";
// echo $sql;
$res = mysqli_query($link , $sql);
if ($res && mysqli_affected_rows($link)) {
return mysqli_affected_rows($link);
}else{
return false;
}
}
/**
* 处理修改方法的字段等于值
* @param array $data 接收到的前台传来的修改数据
* @return string 返回处理后的结果
*/
function praseData($data)
{
foreach ($data as $key => $value) {
$value = praseVal($value);
if (is_scalar($key)) {
$res[] = $key.'='.$value;
}
}
// var_dump($res);
return $res;
}
/**
* 处理数据总条数
* @param object $link 数据库连接
* @param string $tableName 表名
* @param string $field 字段
* @param string $where 条件
* @return $total 返回数据总条数或false
*/
function total($link , $tableName , $field , $where = null)
{
//select count(*) from bbs_user
if (empty($where)) {
$where = null;
}else{
$where = ' where '.$where;
}
$sql = 'SELECT count('.$field.') as count FROM '.$tableName . $where ;
// echo $sql;
$result = mysqli_query($link , $sql);
if ($result && mysqli_affected_rows($link)) {
$total = mysqli_fetch_assoc($result);
// var_dump($total);
return $total['count'];
}else{
return false;
}
}
/**
* 求最小值
* @param object $link 数据库连接
* @param string $tableName 表名
* @param string $field 字段
* @param string $where 条件
* @return $total 返回数据总条数或false
*/
function minimum($link , $tableName , $field , $where = null)
{
//select count(*) from bbs_user
if (empty($field)) {
$field = 'id';
}
if (empty($where)) {
$where = null;
}else{
$where = 'where '.$where;
}
$sql = 'SELECT min('.$field.') as min FROM '.$tableName . $where ;
// echo $sql;
$result = mysqli_query($link , $sql);
if ($result && mysqli_affected_rows($link)) {
$total = mysqli_fetch_assoc($result);
// var_dump($total);
return $total['min'];
}else{
return false;
}
}
/**
* 求最大值
* @param object $link 数据库连接
* @param string $tableName 表名
* @param string $field 字段
* @param string $where 条件
* @return $total 返回数据总条数或false
*/
function maximum($link , $tableName , $field , $where = null)
{
//select count(*) from bbs_user
if (empty($field)) {
$field = 'id';
}
if (empty($where)) {
$where = null;
}else{
$where = 'where '.$where;
}
$sql = 'SELECT max('.$field.') as max FROM '.$tableName . $where ;
// echo $sql;
$result = mysqli_query($link , $sql);
if ($result && mysqli_affected_rows($link)) {
$total = mysqli_fetch_assoc($result);
// var_dump($total);
return $total['max'];
}else{
return false;
}
}
/**
* 求平均值
* @param object $link 数据库连接
* @param string $tableName 表名
* @param string $field 字段
* @param string $where 条件
* @return $total 返回数据总条数或false
*/
function average($link , $tableName , $field , $where = null)
{
//select count(*) from bbs_user
if (empty($field)) {
$field = 'id';
}
if (empty($where)) {
$where = null;
}else{
$where = 'where '.$where;
}
$sql = 'SELECT avg('.$field.') as avg FROM '.$tableName . $where ;
// echo $sql;
$result = mysqli_query($link , $sql);
if ($result && mysqli_affected_rows($link)) {
$total = mysqli_fetch_assoc($result);
// var_dump($total);
return $total['avg'];
}else{
return false;
}
}
/**
* 求和
* @param object $link 数据库连接
* @param string $tableName 表名
* @param string $field 字段
* @param string $where 条件
* @return $total 返回数据总条数或false
*/
function sum($link , $tableName , $field , $where = null)
{
//select count(*) from bbs_user
if (empty($field)) {
$field = 'id';
}
if (empty($where)) {
$where = null;
}else{
$where = 'where '.$where;
}
$sql = 'SELECT sum('.$field.') as sum FROM '.$tableName . $where ;
// echo $sql;
$result = mysqli_query($link , $sql);
if ($result && mysqli_affected_rows($link)) {
$total = mysqli_fetch_assoc($result);
// var_dump($total);
return $total['sum'];
}else{
return false;
}
}
/**
* 分页信息查询
* @param object $link 数据库连接
* @param string $tbName 库名
* @param int $num 每页显示数
* @return $res 返回分页后的查询结果
*/
function page($link , $fields , $tbName , $num=null , $where=null , $orderBy=null , $limit=null)
{
global $pageCount;
//总条数
$count = total($link , '*' , 'bbs_user' , $where=null);
//总页数
$pageCount = ceil($count/$num);
//当前页
$page = isset($_GET['page']) ? $_GET['page'] : 1;
//偏移量
$limit = 'LIMIT '.($page - 1)*$num.' , '.$num;
//查询语句
//select($link , $fields , $tableName , $where=null , $orderBy=null , $limit=null)
$res = select($link , $fields , $tbName , $where=null , $orderBy=null , $limit=null);
return $res;
}
/**
* 输出HTML页面的分页(首页、上一下、下一页、尾页)
* @param int $pageCount 总页数
* @param string $url URL地址
* @return $str 返回HTML信息
*/
function pages($pageCount , $url){
$page = isset($_GET['page']) ? ($_GET['page']):1;
$next = $page - 1;
$prev = $page + 1;
//防止上一页越界
if($next <= 1){
$next = 1;
}
//防止下一页越界
if($prev >= $pageCount)
{
$prev = $pageCount;
}
$str = '<tr><td colspan="9">
<a>当前第'.$page.'页</a>
<a>共 '.$pageCount.' 页 / 第 '.$page.' 页</a>
<a></a>
<a href="'.$url.'?page=1">首页</a>
<a href="'.$url.'?page='.$next.'">上一页</a>
<a href="'.$url.'?page='.$prev.'">下一页</a>
<a href="'.$url.'?page='.$pageCount.'">尾页</a>
</td></tr>';
return$str;
}
/**
* 三表联查
* @param object $link 链接数据库
* @param string $table1 表一
* @param string $table2 表二
* @param string $table3 表三
* @param string $fields 查询目标(表.字段)形式
* @param string $on 链接条件(表1.字段=表2.字段)
* @param string $ons 连接条件(表1.字段=表3.字段)
* @return $data 返回查询结果
*/
function selectN($link , $table1 , $table2 , $table3 , $fields = '*' , $on , $ons , $having=null , $orderBy=null , $limit=null)
{
/*if (empty($groupBy)) {
$groupBy = null;
}else{
$groupBy = 'GROUP BY '.$groupBy;
}*/
if (empty($having)) {
$having = null;
}else{
$having = 'HAVING '.$having;
}
if (empty($orderBy)) {
$orderBy = null;
}else{
$orderBy = 'ORDER BY '.$orderBy;
}
if (empty($limit)) {
$limit = null;
}else{
$limit = $limit;
}
$sql = "SELECT $fields FROM $table1 LEFT JOIN $table2 ON $on LEFT JOIN $table3 ON $ons $having $orderBy $limit";
// echo $sql;
$result = mysqli_query($link, $sql);
if ($result && mysqli_affected_rows($link)) {
while ($rows = mysqli_fetch_assoc($result)) {
$data[] = $rows;
}
return $data;
} else {
return null;
}
}
/**
* 截取中文字符
* @param String $str
* @param Integer $start 从第start个开始
* @param Integer $len 共len个
* @return String
*/
function chinesesubstr($str,$start,$len)
{
$tmpstr = '';
$strlen = $start + $len; //定义需要截取字符的长度
for($i=0;$i<$strlen;$i++){ //使用循环语句,单字截取,并用$tmpstr.=$substr(?,?,?)加起来
if(ord(substr($str,$i,1))>0xa0){
//ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符
$tmpstr .= substr($str,$i,2); //设置tmpstr递加,substr($str,$i,2)的2是指两个字符当一个字符截取(因为两个字符算一个汉字)
$i++;
}else{ //其他情况(英文)按单字符截取
$tmpstr .= substr($str,$i,1);
}
}
return $tmpstr;
}
/**
* 字符串标红,$search值可为数组或字符串
* @param mixed $search 关键词
* @param String $substr 替换源
* @return String
*/
/*function strred($search, $substr)
{
if(is_array($search)){
for($i=0; $i<count($search); $i++){
$substr=str_ireplace($search[$i], '<font color=red>'.$search[$i].'</font>', $substr);
}
}else{
$substr=str_ireplace($search, '<font color=red>'.$search.'</font>', $substr);
}
return $substr;
}*/