温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP对Mysql操作的自定义函数

发布时间:2020-08-10 19:11:54 来源:网络 阅读:1230 作者:DemoHA 栏目:数据库
 <?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;
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI