PHP對Mysql操作的自定義函數(shù)

 <?php

/**
*@name db_connect 連接數(shù)據(jù)庫服務(wù)器
*
*@param string $host 		主機地址
*@param string $user 		用戶名
*@param string $pwd 		用戶密碼
*@param string $name 		數(shù)據(jù)庫名
*@param string $charset 	字符集
*
*@return mixed 數(shù)據(jù)庫連接
*/

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 向數(shù)據(jù)庫插入數(shù)據(jù)
*
*@param string $link 		連接地址
*@param string $table 	表
*@param string $data	 	插入的數(shù)據(jù)
*
*@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 刪除數(shù)據(jù)庫的數(shù)據(jù)
*
*@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 更新數(shù)據(jù)庫的數(shù)據(jù)
*
*@param string $link 		連接地址
*@param string $table 	表
*@param string $set	 		設(shè)置信息
*@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 			刪除數(shù)據(jù)庫的數(shù)據(jù)
*
*@param string $link 		連接地址
*@param string $table 	表
*@param string $where	 條件
*@param string $fields	 	查詢字段
*@param string $where	 條件
*@param string $orderby	 排序
*
*@return mixed      返回數(shù)據(jù)
*/

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;
}



//輔助函數(shù)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;
}


//輔助函數(shù)2:對數(shù)組進行遍歷

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

網(wǎng)頁題目:PHP對Mysql操作的自定義函數(shù)
鏈接地址:http://www.muchs.cn/article0/ishiio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、外貿(mào)建站、小程序開發(fā)云服務(wù)器、標(biāo)簽優(yōu)化建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作