本站即日起正式部署SSL证书启用https安全连接, 联系站长:416326801#qq.com(#改成@)

Latest from the blog

PDO fetch 模式各种参数的输出结果

标签: PHP MySQL

PDO::FETCH_ASSOC 关联数组形式。
PDO::FETCH_NUM 数字索引数组形式。
PDO::FETCH_BOTH 两者数组形式都有,这是默认的。
PDO::FETCH_OBJ 按照对象的形式,类似于以前的mysql_fetch_object()函数。
PDO::FETCH_BOUND 以布尔值的形式返回结果,同时将获取的列值赋给bindParam()方法中指定的变量。
PDO::FETCH_LAZY 以关联数组、数字索引数组和对象3种形式返回结果。

<?php    
$dbAdapter = new PDO("mysql:host=localhost;dbname=test", "root", "1234");    
$dbAdapter->exec("SET NAMES 'utf8';");    
     
 $data = $dbAdapter->query("   
   SELECT id, name, method FROM category   
")->fetchAll(PDO::FETCH_ASSOC);    
    
//var_dump($data);    
/*   
array(   
   array(   
       'id' => '1',   
       'name' => 'HBO',   
       'method' => 'service',   
   ),   
   array(   
       'id' => '2',   
       'name' => '本周新片',   
       'method' => 'movie',   
   ),   
   array(   
       'id' => '3',   
       'name' => '热映中',   
       'method' => 'movie',   
   ),   
)   
*/    
    
    
    
$data = $dbAdapter->query("   
   SELECT name, method FROM category   
")->fetchAll(PDO::FETCH_COLUMN);    
    
//var_dump($data);    
/*   
array(   
   'HBO',   
   '本周新片',   
   '热映中',   
)   
*/    
    
    
    
$data = $dbAdapter->query("   
   SELECT id, name, method FROM category   
")->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC);    
    
//var_dump($data);    
/*   
array(   
   '1' => array(   
       'name' => 'HBO',   
       'method' => 'service',   
   ),   
   '2' => array(   
       'name' => '本周新片',   
       'method' => 'movie',   
   ),   
   '3' => array(   
       'name' => '热映中',   
       'method' => 'movie',   
   ),   
)   
*/    
    
    
    
$data = $dbAdapter->query("   
   SELECT method, id, name FROM category   
")->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC);    
    
//var_dump($data);    
/*   
array(   
   'service' => array(   
       'id' => '1',   
       'name' => 'HBO',   
   ),   
   'movie' => array(   
       'id' => '3',   
       'name' => '热映中',   
   ),   
)   
*/    
    
    
    
$data = $dbAdapter->query("   
   SELECT id, name, method FROM category   
")->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_COLUMN);    
    
//var_dump($data);    
/*   
array(   
   '1' => 'HBO',   
   '2' => '本周新片',   
   '3' => '热映中',   
)   
*/    
    
    
    
$data = $dbAdapter->query("   
   SELECT method, name, id FROM category   
")->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_COLUMN);    
    
//var_dump($data);    
/*   
array(   
   'service' => 'HBO',   
   'movie' => '热映中',   
)   
*/    
    
    
    
    
$data = $dbAdapter->query("   
   SELECT method, id, name FROM category   
")->fetchAll( PDO::FETCH_ASSOC | PDO::FETCH_GROUP);    
    
//var_dump($data);    
/*   
array(   
   'service' => array(   
       array(   
           'id' => '1'   
           'name' => 'HBO'   
       ),   
   )   
   'movie' => array(   
       array(   
         'id' => '2'   
         'name' => '本周新片'   
       ),   
       array(   
         'id' => '3'   
         'name' => '热映中'   
       ),   
   )   
)   
*/    
    
    
    
    
$data = $dbAdapter->query("   
   SELECT method, name, id FROM category   
")->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN);    
    
//var_dump($data);    
/*   
array(   
   'service' => array(   
       'HBO'   
   ),   
   'movie' => array(   
       '本周新片'   
       '热映中'   
   ),   
)   
*/    
    
    
    
    
    
$data = $dbAdapter->query("   
   SELECT id, name, method FROM category   
")->fetchAll(PDO::FETCH_OBJ);    
    
//var_dump($data);    
/*   
array(   
   stdClass{   
       public $id = '1';   
       public $name = 'HBO';   
       public $method = 'service';   
   },   
   stdClass{   
       public $id = '2';   
       public $name = '本周新片';   
       public $method = 'movie';   
   },   
   stdClass{   
       public $id = '3';   
       public $name = '热映中';   
       public $method = 'movie';   
   },   
)   
*/    
    
    
    
    
    
    
    
class Category_1 {}    
    
$data = $dbAdapter->query("   
   SELECT id, name, method FROM category   
")->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, "Category_1");    
    
//var_dump($data);    
/*   
array(   
   Category_1{   
       public $id = '1';   
       public $name = 'HBO';   
       public $method = 'service';   
   },   
   Category_1{   
       public $id = '2';   
       public $name = '本周新片';   
       public $method = 'movie';   
   },   
   Category_1{   
       public $id = '3';   
       public $name = '热映中';   
       public $method = 'movie';   
   },   
),   
*/    
    
    
    
    
    
class Category_2 {    
   public $name;    
   public $method;    
    
   public function __construct() {}    
   public function __set($name, $value ){}    
}    
    
$data = $dbAdapter->query("   
   SELECT id, name, method FROM category   
")->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, "Category_2");    
    
//var_dump($data);    
/*   
array(   
   Category_2{   
       public $name = 'HBO';   
       public $method = 'service';   
   },   
   Category_2{   
       public $name = '本周新片';   
       public $method = 'movie';   
   },   
   Category_2{   
       public $name = '热映中';   
       public $method = 'movie';   
   },   
)   
*/

PHP导入Excel时间格式转换

标签: PHP Excel

public static function excelTime($date, $time=false){
        if(is_numeric($date)) {
                $jd = GregorianToJD(1, 1, 1970);
                $gregorian = JDToGregorian($jd+intval($date)-25569);
                $date = explode('/',$gregorian);
                $date_str = str_pad($date[2],4,'0', STR_PAD_LEFT)
                ."-".str_pad($date[0],2,'0', STR_PAD_LEFT)
                ."-".str_pad($date[1],2,'0', STR_PAD_LEFT)
                .($time?" 00:00:00":'');
                return $date_str;
        }
        return $date;
}

MySQL数据库备份脚本

标签: 备份 MySQL Shell Linux

#!/bin/sh
cd /home/backups/sql/
mkdir /home/backups/extmail
 
# Remove old backups
find /home/backups/sql/* -mtime +5 -exec rm -fr {} \; > /dev/null 2>&1
 
# Create the filename for the backup
eval `date "+day=%d; month=%m; year=%Y; hour=%H; min=%M; sec=%S"`
INSTFIL="extmail-backup-$year$month$day-$hour$min$sec"
 
# Dump the MySQL Database
#touch /home/backups/extmail/$INSTFIL.txt
/usr/local/bin/mysqldump -usqlback -p"sqlbackup" --opt extmail > /home/backups/extmail/$INSTFIL.sql
 
# Gzip the whole folder
 
tar -Pcpzf /home/backups/sql/$INSTFIL.tar.gz /home/backups/extmail/*
 
# Remove the SQL Dump
rm -rf /home/backups/extmail

备份Mysql并上传到ftp服务器

标签: 备份 MySQL Shell Linux

#!/bin/bash

#MySQL设置
MysqlHost=localhost
MysqlUser=root
MysqlPwd=password

#备份文件设置
BackupPath=/backup/db
FileName=$MysqlHost-$(date +%Y%m%d-%H%M%S)
FileFullPath=$BackupPath/$FileName

#ftp设置
FtpHost=10.206.4.121
FtpUser=iptv
FtpPwd=StarIptv
FtpPath=/ftp/backup/mysql

mkdir -p $BackupPath

#备份mysql并导出到文件
mysqldump -u$MysqlUser -p$MysqlPwd -h$MysqlHost --opt --all-databases -f > $FileFullPath.sql

#压缩备份文件
tar -Pzcvf $FileFullPath.tar.gz $FileFullPath.sql

#上传到ftp
ftp -n $FtpHost <<AUTOEOF
quote USER $FtpUser
quote PASS $FtpPwd
cd $FtpPath
binary
put $FileFullPath.tar.gz $FtpPath/$FileName.tar.gz
quit
AUTOEOF

#删除SQL文件
rm -rf $FileFullPath.sql

#删除30天之前的文件
find $BackupPath -type f -mtime +30 -exec rm -f {} \;

«« First 17 18 19 20 21 22