MySQL数据库操作方法封装

 $value) {
			$value = parseValues($value); 
			if (is_scalar($value)) {
				$set[] = $key . '=' . $value;
			}	
		}
		return $set;
	}

	//封装查询
	function select($link , $table , $fields , $where = '')
	{
		if (empty($where)) {
		 	$where = '';
		} else {
			$where = "where $where";
		}
		$sql = "select $fields from $table $where";
		$result = mysqli_query($link , $sql);
		if ($result && mysqli_affected_rows($link)) {
			while ($rows = mysqli_fetch_assoc($result)) {
				$data[] = $rows;
			}
		} else {
			return false;
		}
		return $data;
	}

	//封装最大值
	function big($link , $table , $fields)
	{
		$sql = "select max($fields) as max  from $table ";
		$result = mysqli_query($link , $sql);
		if ($result && mysqli_affected_rows($link)) {
			return mysqli_fetch_assoc($result)['max'];
		} else {
			return false;
		}
	}

	//封装最小值
	function little($link , $table , $fields)
	{
		$sql = "select min($fields) as min  from $table ";
		$result = mysqli_query($link , $sql);
		if ($result && mysqli_affected_rows($link)) {
			return mysqli_fetch_assoc($result)['min'];
		} else {
			return false;
		}
	}
	
	//封装总数
	function total($link , $table , $fields)
	{
		$sql = "select count($fields) as count  from $table ";
		$result = mysqli_query($link , $sql);
		if ($result && mysqli_affected_rows($link)) {
			return mysqli_fetch_assoc($result)['count'];
		} else {
			return false;
		}
	}

	//封装求和
	function sum($link , $table , $fields)
	{
		$sql = "select sum($fields) as sum  from $table ";
		$result = mysqli_query($link , $sql);
		if ($result && mysqli_affected_rows($link)) {
			return mysqli_fetch_assoc($result)['sum'];
		} else {
			return false;
		}
	}

你可能感兴趣的:(PHP,MySQL)