资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

PHP对Mysql操作的自定义函数

 服务器
*
*@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;
}

当前文章:PHP对Mysql操作的自定义函数
本文URL:http://cdkjz.cn/article/ishiio.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220