pdo使用记录

<?php
/**
 * Created by PhpStorm.
 * User: jin
 * Date: 2015/9/25
 * Time: 11:54
 */
class MysqlDb
{
    static $dbArr = array();
    private $conection = null;
    private $dsn = null;
    private $usernName = null;
    private $userPwd = null;
    private $parameter = null;
    function __construct($username,$pwd,$dbname,$host,$parameter = array())
    {
        $this->dsn='mysql:host='.$host.';dbname='.$dbname;
        $this->usernName = $username;
        $this->userPwd = $pwd;
        $this->parameter = $parameter;
        $this->connect();
    }
    private function connect()
    {
        //当初始化对象失败,也就是连接数据库失败时,会抛出PDOException异常
        try{
            //实例化对象
            $this->conection = new PDO($this->dsn,$this->usernName,$this->userPwd,$this->parameter);
            //设置编码
            $this->conection->exec('set names utf8');
            return true;
        }catch(PDOException $e){
            //结束程序,并打印错误信息
            error_log($e->getMessage(), 0);
            return $e->getMessage();
        }
    }
    //写入操作
    function excute($sql,$parameter = array())
    {
        try
        {
            $prepare = $this->conection->prepare($sql);
            $res = $prepare->execute($parameter);
        }
        catch(PDOException $e)
        {
            if($e->errorInfo[0] == 70100 || $e->errorInfo[0] == 2006){
                $count = 0;
                while(!$this->connect()){
                    sleep(1);
                    echo "数据库重新连接失败(try:{$count})\n";
                    $count++;
                };
                $res = $this->excute($sql, $parameter);
            }
            else
            {
                exit($e->errorInfo[2]);
            }
        }
        return $res;
    }
    //查询操作
    function query($sql,$parameter = array())
    {
        try
        {
            $prepare = $this->conection->prepare($sql);
            $prepare->execute($parameter);
            //设置返回的是索引数组
            $prepare->setFetchMode(PDO::FETCH_ASSOC);
            //取出结果数组
            $row = $prepare->fetchAll();
        }
        catch(PDOException $e)
        {
            if($e->errorInfo[0] == 70100 || $e->errorInfo[0] == 2006){
                $count = 0;
                while(!$this->connect()){
                    sleep(1);
                    echo "数据库重新连接失败(try:{$count})\n";
                    $count++;
                };
                $res = $this->query($sql, $parameter);
            }
            else
            {
                exit($e->errorInfo[2]);
            }
        }
        return $row;
    }
    //只查询第一条
    function find($sql,$parameter = array())
    {
        $row = $this->query($sql,$parameter);
        return current($row);
    }
    //关闭连接
    function close()
    {
        $this->conection = null;
    }
    function getLastInsertId()
    {
        return $this->conection->lastInsertId();
    }
}

获取文件时间信息

filemtime ( string filename )

返回文件上次被修改的时间,出错时返回 FALSE。时间以 Unix 时间戳的方式返回,可用于 date()。
例如:

$a=filemtime("log.txt");
echo "修改时间:".date("Y-m-d H:i:s",$a).";

filectime ( string filename )

返回文件上次 inode 被修改的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。
例如:

$a=filectime("log.txt");
echo "创建时间:".date("Y-m-d H:i:s",$a).";

fileatime ( string filename )

返回文件上次被访问的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。
例如:

$a=fileatime("log.txt");
echo "修改时间:".date("Y-m-d H:i:s",$a).";

页面最大过期时间

  1. php.ini
//在php.ini 中设置,如30秒
max_execution_time = 30
  1. [代码]ini_set()函数
//虚拟主机,且服务器允许使用ini_set()函数,可在PHP代码中设置:
ini_set('max_execution_time',864000);

计算字符串的不同

//检测字符串相同的部分
function check_str($str1,$str2){
    static $str_length_flag=1000000000;
    static $flag=0;

    $tmp_str1=$str1;$tmp_str2=$str2;
    $strlen=utf8_strlen($str1);
    $str_temp='';
    for ($i=0; $i <$strlen ; $i++) {
        for ($j=utf8_strlen($str_temp)+1; $j <=$strlen-$i ; $j++) 
        {
            //$j=$str_temp?utf8_strlen($str_temp):$j;
            $str_1=mb_substr($str1, $i, $j, 'utf-8');
            $str_start=strpos($str2, $str_1);
            if (!($str_start===false)) {
                if(strlen($str_1)>strlen($str_temp)){
                    $str_temp=$str_1;
                    $ex_info=array(
                        'str1_left'=>$i,
                        'str1_right'=>$j,
                        'str2_left'=>$str_start,
                        'str2_right'=>$str_start+strlen($str_temp),
                    );
                }
                if(utf8_strlen($str_1)>=utf8_strlen($str1)||utf8_strlen($str_1)>$str_length_flag){
                    break 2;
                }
            }else{
                break;
            }
        }
    }
    if (!$flag) {
        $str_length_flag=utf8_strlen($str_temp);
        $flag=1;
    }
    $res_arr=array();
    if (isset($ex_info)) {
        $str1_left_str=mb_substr($str1, 0, $ex_info['str1_left'], 'utf-8');
        $str2_left_str=substr($str2,0,$ex_info['str2_left']);
        if ($str1_left_str&&$str2_left_str) {
            $strleft=check_str($str1_left_str,$str2_left_str);
            if ($strleft) {
                $res_arr=array_merge($res_arr,$strleft);
            }
        }
        if ($str_temp) 
        {
            $res_arr=array_merge($res_arr,array($str_temp));
        }


        $str1_right_str=mb_substr($str1, $ex_info['str1_left']+$ex_info['str1_right'], utf8_strlen($str1), 'utf-8');
        $str2_right_str=substr($str2,$ex_info['str2_right'],strlen($str2));
        if ($str1_right_str&&$str2_right_str) 
        {
            $strright=check_str($str1_right_str,$str2_right_str);
            if ($strright) {

                $res_arr=array_merge($res_arr,$strright);

            }
        }
    }
    return $res_arr;

}
//改变样式
function str_color($str1,$str2,$arr){
    foreach ($arr as $key => $value) {
        $start1=strpos($str1,$value);
        $str1=substr_replace($str1, '---$'.$key.'$', $start1,strlen($value));
        $start2=strpos($str2,$value);
        $str2=substr_replace($str2, '---', $start2,strlen($value));
    }
    $arr1_temp=explode('---', $str1);
    $arr2_temp=explode('---', $str2);
    $temp_arr=array();
    foreach ($arr1_temp as $key => $value) {
        $check=preg_replace('/^\$(\d+)\$$/', '', $value);
        if (strlen($check)&&strlen($arr2_temp[$key])) {
            $temp_arr[]='<del>'.$arr2_temp[$key].'</del>'.'<ins color="greed">'.$value.'</ins>';//修改
        }elseif(strlen($check)){
            $temp_arr[]='<ins color="greed">'.$value.'</ins>';//新加的
        }elseif(strlen($arr2_temp[$key])){
            $temp_arr[]='<del>'.$value.$arr2_temp[$key].'</del>';//删除的
        }else {
            $temp_arr[]=$value;
        }
    }
    $temp_arr=preg_replace_callback('/(.*)(\$(\d+)\$)(.*)/', function ($matchs) use($arr){
        return $arr[$matchs[3]].$matchs[1].$matchs[4];
    }, $temp_arr);
    return implode('', $temp_arr);
}
$str1='我叫小明';
        $str2='我的名字是小明';
        echo "
        <style>
ins{
text-decoration: none;
padding: 3px 0;
background:
 #EAF0DD;
color: #5B7918;
}
del{text-decoration: line-through;
padding: 3px 0;
background: #f5dfdf;
color: #ac1414;}
        </style>
        ";
        echo(str_color($str2,$str1,check_str($str1,$str2)));