Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created August 23, 2012 05:01
Show Gist options
  • Save leizongmin/3432637 to your computer and use it in GitHub Desktop.
Save leizongmin/3432637 to your computer and use it in GitHub Desktop.
PHP基础操作类库
<?php
/*!
* PHP基础操作类库
*
* @author 老雷 <[email protected]>
* @date 2012-08-23 12:43:45
*/
/**
* MySQL 数据库操作
*/
class SQL {
/**
* 连接到数据库
* 成功返回true, 失败返回false
*
* @param {string} $server
* @param {string} $username
* @param {string} $password
* @param {string} $database
* @return {bool}
*/
public static function connect ($server = 'localhost:3306', $username = 'root', $password = '', $database = '') {
mysql_connect($server, $username, $password);
$r = mysql_select_db($database);
// 设置默认字符集为utf-8
SQL::update('set names utf8');
return $r;
}
/**
* 获取出错信息
* 返回数据格式: {id:出错ID, error:出错描述}
*
* @return {array}
*/
public static function error () {
return array(
'id' => mysql_errno(),
'error' => mysql_error()
);
}
/**
* 返回出错代码
*
* @return {int}
*/
public static function errno () {
return mysql_errno();
}
/**
* 返回出错描述信息
*
* @return {string}
*/
public static function errmsg () {
return mysql_error();
}
/**
* 查询并返回所有数据
* 格式为: [{字段名:值, 字段名:值 ...}, ...],返回FALSE表示失败
*
* @param {string} $sql
* @return {array}
*/
public static function getAll ($sql) {
$r = mysql_query($sql);
if (mysql_errno()) {
return FALSE;
}
$data = array();
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
$data[] = $row;
}
if (count($data) < 1)
return FALSE;
else
return $data;
}
public static function getData ($sql) {
return SQL::getAll($sql);
}
/**
* 查询并返回一行数据 格式为 {字段名:值, 字段名:值 ...},返回FALSE表示失败
*
* @param {string} $sql
* @return {array}
*/
public static function getOne ($sql) {
$sql .= ' LIMIT 1';
$data = SQL::getAll($sql);
if ($data == FALSE)
return FALSE;
else
return $data[0];
}
public static function getLine ($sql) {
return SQL::getOne($sql);
}
/**
* 执行SQL命令 返回影响的记录行数
*
* @param {string} $sql
* @return {int}
*/
public static function update ($sql) {
mysql_query($sql);
return mysql_affected_rows();
}
public static function runSql ($sql) {
return SQL::runSql($sql);
}
/**
* 取最后插入ID
*
* @return {int}
*/
public static function id () {
return mysql_insert_id();
}
public static function lastId () {
return SQL::id();
}
/**
* 转换为SQL安全字符串
*
* @param {string} $str
* @return {string}
*/
public static function escape ($str) {
return mysql_escape_string($str);
}
}
/**
* 调试信息流操作
*/
class DEBUG {
public static $stack = '';
/**
* 添加到DEBUG流
*
* @param {string} $msg
* @param {string} $title
*/
public static function put ($msg = '', $title = '') {
if (!empty($title)) {
$msg = '['.$title.'] '.$msg;
}
DEBUG::$stack .= $msg."\r\n";
}
/**
* 获取DEBUG流
*
* @return {string}
*/
public static function get () {
return DEBUG::$stack;
}
/**
* 清空DEBUG流,并返回之前的信息
*
* @return {string}
*/
public static function clear () {
$ret = DEBUG::$stack;
DEBUG::$stack = '';
return $ret;
}
}
/**
* 上传文件管理
*/
class UPLOAD {
/**
* 获取上传文件
* 返回格式:{name: 名称, type: 文件MIME类型, size: 大小, tmp_name: 临时文件名}
*
* @param {string} $filename
* @return {array}
*/
public static function get ($filename) {
if (isset($_FILES[$filename])) {
$uploaded_file = array(
'name' => $_FILES[$filename]['name'],
'type' => $_FILES[$filename]['type'],
'size' => $_FILES[$filename]['size'],
'tmp_name' => $_FILES[$filename]['tmp_name']
);
} elseif (isset($_POST[$filename])) {
$uploaded_file = array(
'name' => $_POST[$filename],
);
} elseif (isset($GLOBALS['HTTP_POST_FILES'][$filename])) {
global $HTTP_POST_FILES;
$uploaded_file = array(
'name' => $HTTP_POST_FILES[$filename]['name'],
'type' => $HTTP_POST_FILES[$filename]['type'],
'size' => $HTTP_POST_FILES[$filename]['size'],
'tmp_name' => $HTTP_POST_FILES[$filename]['tmp_name']
);
} elseif (isset($GLOBALS['HTTP_POST_VARS'][$filename])) {
global $HTTP_POST_VARS;
$uploaded_file = array(
'name' => $HTTP_POST_VARS[$filename],
);
} else {
$uploaded_file = array(
'name' => $GLOBALS[$filename . '_name'],
'type' => $GLOBALS[$filename . '_type'],
'size' => $GLOBALS[$filename . '_size'],
'tmp_name' => $GLOBALS[$filename]
);
}
return $uploaded_file;
}
/**
* 移动临时文件
*
* @param {array} $file
* @param {string} $target
* @return {string}
*/
public static function move ($file, $target) {
if (substr($target, -1) != '/') $target .= '/';
$target .= $file['name'];
move_uploaded_file($file['tmp_name'], $target);
return $target;
}
}
/**
* 调试输出
*
* @param {mixed} $var
*/
function dump ($var) {
echo '<pre>';
print_r($var);
echo '</pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment