您的位置:首页 > 编程语言 > PHP开发

PHP读取配置文件、mysqli操作、session读写、url跳转

2016-08-06 01:19 579 查看
<?php

/**
* 获取、设定配置文件的值
* @param unknown_type $key
* @param unknown_type $val
*/
function getConfig($key=null,$val=null) {
$config = array();
if (empty($config)){
$config = include ("config.php");
}
if (is_null($key)) {
return $config;
} else {
if (is_null($val)) {
//获取指定值
if (strpos($key, "->")) {
$tmp = $config;
$arr = explode("->", $key);
foreach ($arr as $v) {
$tmp = $tmp[$v];
}
return $tmp;
} else {
return $config[$key];
}
} else {
//设定指定值
if (strpos($key, "->")){
$arr = explode("->", $key);
$config[$arr[0]][$arr[1]] = $val;
} else {
$config[$key] = $val;
}
return true;
}
}
}

/**
* 获取数据库连接
*/
function connectDB() {
static $model = null;
if (is_null($model)) {
$dbInfo = getConfig("db");
$model = new mysqli($dbInfo["host"],$dbInfo["user"],$dbInfo["passwd"],$dbInfo["dbname"]);
if ($model->connect_errno) {
die("数据库连接错误".$model->connect_errno." err:".$model->connect_error);
}
$model->set_charset("utf8");
}
return $model;
}
/**
* 查询数据库内容
* @param unknown_type $sql
* @param unknown_type $fieldCount 字段数
* @param unknown_type $type 字段类型
* @param unknown_type $param
*/
function select($sql,$fieldCount=1,$type="",$param=null) {
$funStr = '$stmt = mysqli_prepare(connectDB(),"'.$sql.'");';
if (empty($type)){
$funStr = $funStr.'mysqli_stmt_bind_param($stmt);';
} else {
for ($i=0;$i<count($param);$i++) {
$paramArray[] = '$p'.$i;
$funStr = $funStr.'$p'.$i.'="'.$param[$i].'";';
}
$funStr = $funStr.'mysqli_stmt_bind_param($stmt,"'.$type.'",'. implode(',', $paramArray).');';
}
$funStr = $funStr.'mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);';
for ($i=0;$i<$fieldCount;$i++) {
$bindResult[] = '$r'.$i;
}
$funStr = $funStr.'mysqli_stmt_bind_result($stmt,'.implode(",", $bindResult).');
while (mysqli_stmt_fetch($stmt)){
$result[] = array('.implode(",", $bindResult).');
}
return $result;';
$fun = create_function('$s', $funStr);
return $fun();
}

/**
* 更新数据库内容
* @param $sql
* @param $type
* @param $param
*/
function update($sql,$type="",$param=null) {
$funStr = '$stmt = mysqli_prepare(connectDB(),"'.$sql.'");';
if (empty($type)){
$funStr = $funStr.'mysqli_stmt_bind_param($stmt);';
} else {
for ($i=0;$i<count($param);$i++) {
$paramArray[] = '$p'.$i;
$funStr = $funStr.'$p'.$i.'="'.$param[$i].'";';
}
$funStr = $funStr.'mysqli_stmt_bind_param($stmt,"'.$type.'",'. implode(',', $paramArray).');';
}
$funStr = $funStr.'mysqli_stmt_execute($stmt);
if (mysqli_stmt_affected_rows($stmt)==1){
return true;
} else {
return false;
}';
$fun = create_function('$s', $funStr);
return $fun();
}

/**
* 获取、设定session值
* @param $key
* @param $val
*/
function session($key=null,$val=null) {
$prefix = getConfig("session->prefix");
if (is_null($key)) {
return $_SESSION[$prefix];
} else {
if (is_null($val)) {
//获取指定值
return $_SESSION[$prefix][$key];
} else {
//设定指定值
$_SESSION[$prefix][$key] = $val;
return true;
}
}
}

/**
* 跳转到URL
* @param unknown_type $url
*/
function jump($url="index.php") {
$jumpUrl = "<script>location.href='".$url."'</script>";
die($jumpUrl);
}

/**
* url添加参数
* @param unknown_type $param
*/
function url($param=null) {
if (is_null($param)) {
$url="";
} else {
$url="?".http_build_query($param);
}
return "index.php".$url;
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: