a24hhsj
9/10/2018 - 2:58 AM

php_base

#数据库相关操作
/******************************************************************************************************/
/******************************************************************************************************/
#创建数据库非持久连接
function manageDbConnect($db, $con)
{
	if ($con)
	{
		return $con;
	}
	try{
		#打开一个MySQL服务器的非持久连接
		$resouce = mysql_connect($db['host'], $db['username'], $db['password']);
		if (!$resouce)
		{
			$mysqlError = mysql_error();
			error_log("mysql error: {$mysqlError}");
		}
	}
	catch(Exception $e)
	{
		#发生不可预知的异常时,关闭数据库连接
		@mysql_close();
	}

	return $resouce;
}

#选择数据库
function manageDbSelect($name)
{
	global $dataCon;
	$ret = false;
	try{
		if($dataCon)
		{
			#设置活动数据库
			$ret = mysql_select_db($name, $dataCon);
			mysql_query("SET NAMES UTF8");
		}
	}
	catch(Exception $e)
	{
		$mysqlError = mysql_error();
		error_log("mysql error: {$mysqlError}");
	}
	return $ret;
}

#执行sql语句
function manageDbExcute($sql, $name)
{
	global $dataCon;
	try{
		$result = false;
		if ($dataCon)
		{
			#设置活动数据库
			mysql_select_db($name, $dataCon);
			mysql_query("SET NAMES UTF8");
			$result = mysql_query($sql, $dataCon);
		}
		return $result;
	}
	catch(Exception $e)
	{
		$mysqlError = mysql_error();
		error_log("mysql error: {$mysqlError}");
	}
}

#获取记录集
function manageDbGetRows($sql, $name)
{
	global $dataCon;
    $data = array();
    $ret = array('error'=>1, 'data'=>$data);
	try{
		if ($dataCon)
		{
			#设置活动数据库
			mysql_select_db($name, $dataCon);
			mysql_query("SET NAMES UTF8");
			#获取记录集
			$sqlResult = mysql_query($sql, $dataCon);
			if ($sqlResult == FALSE)
			{
				$mysqlError = mysql_error();
				error_log("mysql error: {$mysqlError}");
			} 
			else 
			{
                $num = mysql_num_rows($sqlResult);
                $ret['error'] = 0;
				if ($num > 0)
				{
					while($row=mysql_fetch_array($sqlResult))
					{
						array_push($data, $row);
					}
                }
                $ret['data'] = $data;
			}
		}	
	}
	catch(Exception $e)
	{
		$mysqlError = mysql_error();
		error_log("mysql error: {$mysqlError}");
	}
	return $ret;	
}

#关闭数据库连接
function manageDbClose()
{
	global $dataCon;
	if ($dataCon)
	{
		@mysql_close($dataCon);
		#echo 'close mysql';
	}
}
/*****************************************************************************************************************/
/*****************************************************************************************************************/
function curl($url, $params = false, $ispost = 0, $https = 0)
{
    $httpInfo = array();
    $ch = curl_init();
    #curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);         #http版本
    #curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');     #在HTTP请求中包含一个"User-Agent: "头的字符串
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);               #连接等待时间
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);                      #超时时间
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             #返回原生(RAW)输出,PHP5.1.3开始,默认返回原生输出
    if ($https) 
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);            #对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);            #从证书中检查SSL加密算法是否存在
    }
    if ($ispost)
    {
        curl_setopt($ch, CURLOPT_POST, true);                       #post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);              #post参数
        curl_setopt($ch, CURLOPT_URL, $url);                        #请求的url
    }
    else
    {
        if ($params)
        {
            if (is_array($params))
            {
                $params = http_build_query($params);
            }
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        }
        else
        {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }

    $response = curl_exec($ch);

    if ($response === FALSE)
    {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}
#生成兑换码
function createMarkCode($jxmid)
{
    $monArr = Array('J', 'F', 'M', 'P', 'A', 'J', 'L', 'G', 'S', 'O', 'N', 'D');       #月份编码

    $curTime = time();

    $year = date('Y', $curTime);
    $yearA = ($year + 3) % 10;      #年份+3后个位数
    $yearB = floor(($year + 3) % 100 / 10);      #年份+3后十位数
    $index = rand(0, 1);
    if ($index == 0)
    {
        $yearA = chr(rand(65,90));      #随机大写字母
    }
    else
    {
        $yearB = chr(rand(65,90));
    }
    $yearStr = $yearA.$yearB;
    
    #月份用大写字母表示
    $month = date('m', $curTime);
    $monStr = $monArr[$month];      
    #日期的转换
    $today = date('d', $curTime);
    if ($today > 24)
    {
        $dayStr = chr(48 + $today - 24 - 1);         #25~31对应0~6
    }
    else
    {
        $dayStr = chr(65 + $today - 1);         #1~24对应A~Z
    }

    #ID的变换
    $idStr = $jxmid + 12586;
    #随机大写字母
    $tmp = chr(rand(65,90));
    #年份+月份+id+日期+随机
    $code = $yearStr.$monStr.$idStr.$dayStr.$tmp;

    return $code;
}