PHP常用函数,打印日志errorLog

125 阅读1分钟

PHP常用函数,打印日志errorLog

`/** * 返回数据中指定的一列 * @access public * @param mixed columnKey键名@paramnullcolumnKey 键名 * @param null indexKey 作为索引值的列 * @return array */ if (!function_exists('array_column')) { function array_column(items,items,columnKey, indexKey = null) { // if (function_exists('array_column')) { // return array_column(items, columnKey,columnKey, indexKey); // }

        $result = array();
        foreach ($items as $row) {
            $key    = $value = null;
            $keySet = $valueSet = false;

            if (null !== $indexKey && array_key_exists($indexKey, $row)) {
                $key    = (string) $row[$indexKey];
                $keySet = true;
            }

            if (null === $columnKey) {
                $valueSet = true;
                $value    = $row;
            } elseif (is_array($row) && array_key_exists($columnKey, $row)) {
                $valueSet = true;
                $value    = $row[$columnKey];
            }

            if ($valueSet) {
                if ($keySet) {
                    $result[$key] = $value;
                } else {
                    $result[] = $value;
                }
            }
        }

        return $result;
    }
}	
if (!function_exists('getFields')) {
	// SELECT COLUMN_NAME FROM information_schema.`COLUMNS` WHERE TABLE_NAME = 'v_enterpriseinfo';
	function getFields($table_name=''){
	    // $table_name='v_enterpriseinfo';
	    $result=array();
	    if($table_name==''){
	        return $result;
	    }
	    global $db_type,$db;
	    $_password_sql = "SELECT COLUMN_NAME FROM information_schema.`COLUMNS` WHERE TABLE_NAME = '$table_name'";
	    $statement = $db->prepare(check_sql($_password_sql));
	    $statement->execute();
	    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
	    $result=array_column($result,'COLUMN_NAME');
	    // xdug($result);
	    return $result;
	}
}
if (!function_exists('errorLog')) {
		//errorLog(G('begin','end',6),'time.log');
	/**
	 * 打印错误日志
	 * 如果后缀为php的,需要删除后重新生成,其他后缀的,重复写入,手动删除日志
	 * 
	 */
	function errorLog($message='',$file='123.log')
	{
	   $log_dir=$_SERVER['DOCUMENT_ROOT']."/log/".date('Ymd')."/";
	    //$log_dir=CACHE_ROOT."/log/".date('Y-m-d')."/";
	    // $log_dir=dirname(__FILE__)."/";
	    // echo $log_dir;die;
	    if(!is_dir($log_dir)){
	        @mkdir($log_dir,0777,true);
	    }
	    $file=$log_dir.$file;
	    if(is_array($message)){
	        $arr=explode(".",$file);
	        if($arr[1]=='php'){
	            error_log("<?php \n return ".var_export($message, true)."\n", 3,$file);
	        }else{
	             error_log(var_export($message, true)."\n", 3,$file);
	        }
	        
	    }else{
	       error_log($message."\n\n", 3,$file); 
	    }
	   // xdug($message);
	    // error_log($message, 3,$file);
	   
	}
}
if (!function_exists('check_str')) {
	function check_str($string) {
		global $db_type,$db;
		//when code in db is urlencoded the ' does not need to be modified
		if ($db_type == "sqlite") {
			if (function_exists('sqlite_escape_string')) {
				$string = sqlite_escape_string($string);
			}
			else {
				$string = str_replace("'","''",$string);
			}
		}
		if ($db_type == "pgsql") {
			$string = pg_escape_string($string);
		}
		if ($db_type == "mysql") {
            if(function_exists('mysql_real_escape_string')){
                $tmp_str = mysql_real_escape_string($string);
            }
            else{
                $tmp_str = mysqli_real_escape_string($db, $string);
            }
            if (strlen($tmp_str)) {
                $string = $tmp_str;
            }
            else {
                $search = array("\x00", "\n", "\r", "\\", "'", "\"", "\x1a");
                $replace = array("\\x00", "\\n", "\\r", "\\\\" ,"\'", "\\\"", "\\\x1a");
                $string = str_replace($search, $replace, $string);
            }
		}
		return trim($string); //remove white space
	}
}

`