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

Latest from the blog

PHP循环创建文件夹

标签: PHP

/*
 * 功能:循环检测并创建文件夹 
 * 参数:$path 文件夹路径 
 * 返回: 
 */

public static function createDir($path) {
    if( !file_exists($path) ) {
        self::createDir(dirname($path));
        mkdir($path, 0755);
    }
}

php基于base64解码数据与图片的转换

标签: PHP

<?php
header('Content-type:text/html;charset=utf-8');
//读取图片文件,转换成base64编码格式
$image_file = './4296762_165319032930_2.jpg';
$image_info = getimagesize($image_file);
// $image_info = '/9j/4AAQSkZJRgABAQAASABIAAD......';
$base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($image_file)));
// $base64_image_content = 'data:image/jpg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD......';

//保存base64字符串为图片
//匹配出图片的格式
// $base64_image_content = 'data:image/jpg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD......';
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
  $type = $result[2];
  $new_file = "./test.{$type}";
  // base64_decode('/9j/4AAQSkZJRgABAQAASABIAAD......');
  if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
    echo '新文件保存成功:', $new_file;
  }

}
?>
<img src="<?php echo $base64_image_content;?>" />

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';   
   },   
)   
*/
«« First 17 18 19 20 21 22 23 ... Next »