/*
* ------------------------------连接函数--------------------------------
*/
/**
* 连接sql_server数据库
* @param arr $data
*/
function sp_sqlserver() {
$serverName = C('DB_HOST'); //数据库服务器地址
$uid = C('DB_USER'); //数据库用户名
$pwd = C('DB_PWD'); //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>C('DB_NAME'));
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn == false){
sp_error("数据库连接失败");
} else {
return $conn;
}
}
/**
* 字符转换
* @param arr $data
*/
function sp_charcon($data) {
if ($data) {
foreach ($data as $k=>$v) {
$data[$k] = mb_convert_encoding($v, "GBK", "UTF-8");
}
}
return $data;
}
/**
* 字符转换 GBK转UTF-8
* @param arr $data
*/
function sp_charcon_reverse($data) {
if ($data) {
foreach ($data as $k=>$v) {
$data[$k] = mb_convert_encoding($v, "UTF-8", "GBK");
}
}
return $data;
}
/*
* ------------------------------查询函数--------------------------------
*/
/**
* sqlsrv 普通查询
*/
function sp_srvquery($table,$fields="*",$order='id',$sec='desc',$where='1=1') {
$return = array();//连接数据库
$sql = " SELECT {$fields} ";
$sql .= " FROM {$table}";
$sql .= " WHERE {$where} ";
$sql .= " ORDER BY {$order} {$sec}";
echo $sql;
$conn = sp_sqlserver();
$res = sqlsrv_query($conn, $sql);
while ($data = sqlsrv_fetch_array($res,SQLSRV_FETCH_ASSOC)) {
$data = sp_charcon_reverse($data);
$return[] = $data;
}
return $return;
}
/**
* sqlsrv 分页查询
*/
function sp_srvlimit($table,$fields="*",$page=1,$limit=10,$where='') {
$return = array();//连接数据库
$sql = " SELECT TOP {$limit} {$fields} ";
$sql .= " FROM {$table}";
$sql .= " WHERE (id NOT IN";
$sql .= " (SELECT TOP ({$limit}*{$page}) id";
$sql .= " FROM {$table}";
$sql .= " ORDER BY id)) {$where} ";
$sql .= " ORDER BY id";
$conn = sp_sqlserver();
$res = sqlsrv_query($conn, $sql);
while ($data = sqlsrv_fetch_array($res,SQLSRV_FETCH_ASSOC)) {
$data = sp_charcon_reverse($data);
$return[] = $data;
}
return $return;
}
/**
* sqlsrv 统计查询
*/
function sp_srvcount($table,$fields="*",$where='1=1') {
$sql = " SELECT COUNT({$fields}) as count ";
$sql .= " FROM {$table}";
$sql .= " WHERE ";
$sql .= " {$where} ";
$conn = sp_sqlserver();
$res = sqlsrv_query($conn, $sql);
$data = sqlsrv_fetch_array($res,SQLSRV_FETCH_ASSOC);
return $data['count'];
}
/**
* sqlsrv 双表联合查询
*/
function sp_srvjoin($table_1,$table_2,$param_1=array(),$param_2=array(), $field, $where="1=1") {
$return = array();
//拼接查询字段
if ($param_1) {
$str1 = "";
foreach ($param_1 as $k=>$v) {
$str1 .= "a." . $v . ",";
}
$str1 = substr($str1,0,-1);
} else {
$str1 = "a.*";
}
if ($param_2) {
$str2 = "";
foreach ($param_2 as $k=>$v) {
$str2 .= "b." . $v . ",";
}
$str2 = substr($str2,0,-1);
} else {
$str2 = "b.*";
}
$sql = " SELECT {$str1},{$str2}";
$sql .= " FROM {$table_1} a";
$sql .= " INNER JOIN {$table_2} b";
$sql .= " ON a.{$field} = b.{$field}";
$sql .= " where {$where} ";
$conn = sp_sqlserver();
$res = sqlsrv_query($conn, $sql);
while ($data = sqlsrv_fetch_array($res,SQLSRV_FETCH_ASSOC)) {
$data = sp_charcon_reverse($data);
$return[] = $data;
}
return $return;
}
PHP_sqlserver数据库函数
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- MySQL:实例:查询一个数据库的所有非空表,并按所含记录数降序排列; select table_schema,...
- 最近因工作原因,要用安装sqlserver2012(传送门),摒弃掉mysql切换到sqlserver还真是很折腾...
- mySQLi连接数据库:(仅适用于PHP5以上版本,PHP5一下请使用mySQL连接) 1、面向过程方法 //连接...