码迷,mamicode.com
首页 > 数据库 > 详细

PHP对Mysql操作的自定义函数

时间:2016-08-29 22:51:28      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:php   mysql   

 <?php

/**
*@name db_connect 连接数据库服务器
*
*@param string $host 		主机地址
*@param string $user 		用户名
*@param string $pwd 		用户密码
*@param string $name 		数据库名
*@param string $charset 	字符集
*
*@return mixed 数据库连接
*/

function db_connect($host,$user,$pwd,$name,$charset)
{
	$link = mysqli_connect($host,$user,$pwd);
	if (!$link) {
		return false;
	}
 
	if (!mysqli_select_db($link,$name)) {
		return false;
	}
	mysqli_set_charset($link,$charset);
	

	return $link;
}

 
/**
*@name db_insert 向数据库插入数据
*
*@param string $link 		连接地址
*@param string $table 	表
*@param string $data	 	插入的数据
*
*@return mixed true或者false
*/

 
function db_insert($link,$table,$data)
{
	$keys = join(‘,‘, array_keys($data));
	$values = implode(‘,‘, parse_value(array_values($data)));
	
	$sql = "insert into $table($keys) values($values)";
//echo $sql;die;
	$result = mysqli_query($link, $sql);
	if ($result && mysqli_affected_rows($link)) {
		//返回本次插入的id(该表有自增的id字段)
		return mysqli_insert_id($link);
	}  
	return false;
}


/**
*@name db_delete 删除数据库的数据
*
*@param string $link 		连接地址
*@param string $table 	表
*@param string $where	 条件
*
*@return mixed      true或者false
*/
function db_delete($link,$table,$where)
{
	$sql = "delete from $table where $where";
	
	$result = mysqli_query($link,$sql);
	if ($result && mysqli_affected_rows($link)) {
		return true;
	}
	return false;
}

/**
*@name db_delete 更新数据库的数据
*
*@param string $link 		连接地址
*@param string $table 	表
*@param string $set	 		设置信息
*@param string $where	 条件
*
*@return mixed      true或者false
*/
function db_update($link,$table,$set,$where)
{
	if (is_array($set)) {
		$set = join(‘,‘, parse_set($set));
	}
	$sql = "update $table set $set where $where";
	
	$result = mysqli_query($link, $sql);
	if ($result && mysqli_affected_rows($link)) {
		return true;
	}
	return false;
}

/**
*@name db_delete 			删除数据库的数据
*
*@param string $link 		连接地址
*@param string $table 	表
*@param string $where	 条件
*@param string $fields	 	查询字段
*@param string $where	 条件
*@param string $orderby	 排序
*
*@return mixed      返回数据
*/

function db_select($link,$table,$fields, $where=null, $orderby=null)
{
	if (is_array($fields)) {
		$fields = implode(‘,‘,$fields);
	}
	$sql = "select $fields from $table";
	
	if ($where) {
		$sql .= " where $where";
	}
	
	if ($orderby) {
		$sql .= " order by $orderby";
	}
	
	$result = mysqli_query($link,$sql);
	
	if ($result && mysqli_affected_rows($link)) {
		while ($row = mysqli_fetch_assoc($result)) {
			$data[] = $row;
		}
		return $data;
	} 
	return false;
}



//辅助函数1:对字符类型进行处理



function parse_value($data)
{
	if (is_string($data)) {
		$data = ‘\‘‘ . $data . ‘\‘‘;
	} else if (is_array($data)) {
		$data = array_map(‘parse_value‘, $data);
	} else if (is_null($data)) {
		$data = ‘null‘;
	}
	return $data;
}


//辅助函数2:对数组进行遍历

function parse_set($set)
{
	foreach ($set as $key => $value) {
		$data[] = $key . ‘=‘ . parse_value($value);
	}
	
	return $data;
}


本文出自 “你好我是森林” 博客,请务必保留此出处http://chensenlin.blog.51cto.com/10559465/1844018

PHP对Mysql操作的自定义函数

标签:php   mysql   

原文地址:http://chensenlin.blog.51cto.com/10559465/1844018

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!