明霞山资源网 Design By www.htccd.com
本文实例讲述了PHP常用的类封装。分享给大家供大家参考,具体如下:
这4个类分别是Mysql类、 分页类、缩略图类、上传类。
Mysql类
<"set names ".$cfg['charset']);//设置字符集
}
return self::$link;
}
/**
* 执行一条sql语句
* @param str $sql 查询语句
* @return obj 结果集对象
*/
public static function query($sql){
return self::conn()->query($sql);
}
/**
* 获取多行数据
* @param str $sql 查询语句
* @return arr 多行数据
*/
public static function getAll($sql){
$data = array();
$res = self::query($sql);
while($row = $res->fetch_assoc()){
$data[] = $row;
}
return $data;
}
/**
* 获取一行数据
* @param str $row 查询语句
* @return arr 单行数据
*/
public static function getRow($row){
$res = self::query($sql);
return $res->fetch_assoc();
}
/**
* 获取单个结果
* @param str $sql 查询语句
* @return str 单个结果
*/
public static function getOne($sql){
$res = self::query($sql);
$data = $res->fetch_row();
return $data[0];
}
/**
* 插入/更新数据
* @param str $table 表名
* @param arr $data 插入/更新的数据
* @param str $act insert/update
* @param str $where 更新条件
* @return bool 插入/更新是否成功
*/
public static function exec($table,$data,$act='insert',$where='0'){
//插入操作
if($act == 'insert'){
$sql = 'insert into '.$table;
$sql .= ' ('.implode(',',array_keys($data)).')';
$sql .= " values ('".implode("','",array_values($data))."')";
}else if($act == 'update'){
$sql = 'update '.$table.' set ';
foreach ($data as $k => $v) {
$sql .= $k.'='."'$v',";
}
$sql = rtrim($sql,',');
$sql .= ' where 1 and '.$where;
}
return self::query($sql);
}
/**
* 获取最近一次插入的主键值
* @return int 主键
*/
public static function getLastId(){
return self::conn()->insert_id;
}
/**
* 获取最近一次操作影响的行数
* @return int 影响的行数
*/
public static function getAffectedRows(){
return self::conn()->affected_rows;
}
/**
* 关闭数据库连接
* @return bool 是否关闭
*/
public static function close(){
return self::conn()->close();
}
}
"htmlcode">
<"htmlcode">
<"image".$this->getType();
$method($image,$this->thumbPath);
}
/**
* 进行绘画操作
* @return [type] [description]
*/
public function draw(){
if(!($type = $this->getType())){
echo "文件类型不支持";
return ;
}
//创建大图和小图的画布
$method = "imagecreatefrom".$type;
$bigCanvas = $method($this->sourcePath);
$smallCanvas = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
//创建白色画笔,并给小图画布填充背景
$white = imagecolorallocate($smallCanvas, 255, 255, 255);
imagefill($smallCanvas, 0, 0, $white);
//计算大图的缩放比例
$rate = $this->calculateRate();
//计算大图缩放后的大小信息
$info = $this->getImageSizeByRate($rate);
//进行缩放
imagecopyresampled($smallCanvas, $bigCanvas,
($this->thumbWidth - $info['w']) / 2 , ($this->thumbHeight - $info['h']) / 2,
0, 0, $info['w'], $info['h'], $this->sourceWidth, $this->sourceHeight);
//保存成文件
$this->saveFile($smallCanvas);
//销毁画布
imagedestroy($bigCanvas);
imagedestroy($smallCanvas);
}
}
"htmlcode">
<meta charset="utf8"/>
<"没有上传文件";
return;
}
$info = $this->getInfo($flag);
if(!$this->checkExt($info['name'])){
echo "不支持的文件类型";
return;
}
if(!$this->checkSize($info['size'])){
echo "文件大小超过限制";
return;
}
$filename = $this->randName().'.'.$this->getExt($info['name']);
$dir = $this->createDir();
if(!move_uploaded_file($info['tmp_name'], $dir.'/'.$filename)){
echo "文件上传失败";
}else{
return array('filename'=>$filename,'dir'=>$dir);
}
}
}
"_blank" href="//www.jb51.net/Special/84.htm">php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP网络编程技巧总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
标签:
PHP,封装,工具类
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。