1、记录 SQL 查询语句
// 开启 log
DB::connection()->enableQueryLog();
// 获取已执行的查询数组
DB::getQueryLog();

2、数据库事务处理
// 开启事务 如果连接的不是默认库 一定要加上connection()
DB::beginTransaction();
// 回退事务 如果连接的不是默认库 一定要加上connection()
DB::rollBack();
// 提交事务 如果连接的不是默认库 一定要加上connection()
DB::commit();

3、查询数据集单列的数据
DB::table(‘name’)->pluck(‘column’);

4、Order By, Group By, 和 Having
$users = DB::table(‘users’)
->orderBy(‘name’, ‘desc’)
->groupBy(‘count’)
->having(‘count’, ‘>’, 100)
->get();
DB::table(‘name’)->orderBy(‘column’)->get();
DB::table(‘name’)->orderBy(‘column’,’desc’)->get();
DB::table(‘name’)->having(‘count’, ‘>’, 100)->get();

5、原始表达句
$users = DB::table(‘users’)
->select(DB::raw(‘count() as user_count, status’))
->where(‘status’, ‘<>’, 1)
->groupBy(‘status’)
->get();
// 返回行
DB::select(‘select from users where id = ?’, array(‘value’));
DB::insert(‘insert into foo set bar=2’);
DB::update(‘update foo set bar=2’);
DB::delete(‘delete from bar’);
// 返回 void
DB::statement(‘update foo set bar=2’);
// 在声明语句中加入原始的表达式
DB::table(‘name’)->select(DB::raw(‘count(*) as count, column2’))->get();

Inserts / Updates / Deletes / Unions / Pessimistic Locking
// 插入
DB::table(‘users’)->insert(
[‘email’ => ‘john@example.com’, ‘votes’ => 0]
);
$id = DB::table(‘users’)->insertGetId(
[‘email’ => ‘john@example.com’, ‘votes’ => 0]
);
DB::table(‘users’)->insert([
[‘email’ => ‘taylor@example.com’, ‘votes’ => 0],
[‘email’ => ‘dayle@example.com’, ‘votes’ => 0]
]);
// 更新
DB::table(‘users’)
->where(‘id’, 1)
->update([‘votes’ => 1]);
DB::table(‘users’)->increment(‘votes’);
DB::table(‘users’)->increment(‘votes’, 5);
DB::table(‘users’)->decrement(‘votes’);
DB::table(‘users’)->decrement(‘votes’, 5);
DB::table(‘users’)->increment(‘votes’, 1, [‘name’ => ‘John’]);
// 删除
DB::table(‘users’)->where(‘votes’, ‘<’, 100)->delete();
DB::table(‘users’)->delete();
DB::table(‘users’)->truncate();
// 集合
// unionAll() 方法也是可供使用的,调用方式与 union 相似
$first = DB::table(‘users’)->whereNull(‘first_name’);
$users = DB::table(‘users’)->whereNull(‘last_name’)->union($first)->get();
// 消极锁
DB::table(‘users’)->where(‘votes’, ‘>’, 100)->sharedLock()->get();
DB::table(‘users’)->where(‘votes’, ‘>’, 100)->lockForUpdate()->get();

7、PHP生成唯一有序ID
// 使用php自带uniqid()方法
static public function getUniId ($prefix = ‘e‘, $suffixLength = 8)
{
return uniqid($prefix, false) . ‘‘ . Utils::generateRandomCode($suffixLength, ‘ALL’);
}

// 随机数
static public function generateRandomCode ($len = 6, $format = ‘NUMBER’)
{
switch ($format) {
case ‘ALL’:
$chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789’;
break;
case ‘CHAR’:
$chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz’;
break;
case ‘NUMBER’:
$chars = ‘0123456789’;
break;
default :
$chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789’;
break;
}
// mt_srand((double)microtime() 1000000 getmypid());
$code = “”;
while (strlen($code) < $len) {
$code .= substr($chars, (mt_rand() % strlen($chars)), 1);
}
return $code;
}

// 由于生成的时候时间微秒级别 程序执行太快 要在getUniId()前加上 若干个前导0

8、php如何判断设备是不是移动端
<?php
function isMobile()
{
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset ($_SERVER[‘HTTP_X_WAP_PROFILE’]))
{
return true;
}
// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset ($_SERVER[‘HTTP_VIA’]))
{
// 找不到为flase,否则为true
return stristr($_SERVER[‘HTTP_VIA’], “wap”) ? true : false;
}
// 脑残法,判断手机发送的客户端标志,兼容性有待提高
if (isset ($_SERVER[‘HTTP_USER_AGENT’]))
{
$clientkeywords = array (‘nokia’,
‘sony’,
‘ericsson’,
‘mot’,
‘samsung’,
‘htc’,
‘sgh’,
‘lg’,
‘sharp’,
‘sie-‘,
‘philips’,
‘panasonic’,
‘alcatel’,
‘lenovo’,
‘iphone’,
‘ipod’,
‘blackberry’,
‘meizu’,
‘android’,
‘netfront’,
‘symbian’,
‘ucweb’,
‘windowsce’,
‘palm’,
‘operamini’,
‘operamobi’,
‘openwave’,
‘nexusone’,
‘cldc’,
‘midp’,
‘wap’,
‘mobile’
);
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match(“/(“ . implode(‘|’, $clientkeywords) . “)/i”, strtolower($_SERVER[‘HTTP_USER_AGENT’])))
{
return true;
}
}
// 协议法,因为有可能不准确,放到最后判断
if (isset ($_SERVER[‘HTTP_ACCEPT’]))
{
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER[‘HTTP_ACCEPT’], ‘vnd.wap.wml’) !== false) && (strpos($_SERVER[‘HTTP_ACCEPT’], ‘text/html’) === false || (strpos($_SERVER[‘HTTP_ACCEPT’], ‘vnd.wap.wml’) < strpos($_SERVER[‘HTTP_ACCEPT’], ‘text/html’))))
{
return true;
}
}
return false;
}
?>

9、表单验证
/**

  • 页面作用:常用表单验证类

*/

class class_post

{

//验证是否为指定长度的字母/数字组合

function fun_text1($num1,$num2,$str)
{
return (preg_match(“/^[a-zA-Z0-9]{“.$num1.”,”.$num2.”}$/“,$str))?true:false;
}

//验证是否为指定长度数字

function fun_text2($num1,$num2,$str)
{
return (preg_match(“/^[0-9]{“.$num1.”,”.$num2.”}$/i”,$str))?true:false;
}

//验证是否为指定长度汉字

function fun_font($num1,$num2,$str)
{
// preg_match(“/^[\xa0-\xff]{1,4}$/“, $string);
return (preg_match(“/^([\x81-\xfe][\x40-\xfe]){“.$num1.”,”.$num2.”}$/“,$str))?true:false;

}

//验证身份证号码

function fun_status($str)
{
return (preg_match(‘/(^([\d]{15}|[\d]{18}|[\d]{17}x)$)/‘,$str))?true:false;
}
//验证邮件地址

function funemail($str){
return (preg_match(‘/^[.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/‘,$str))?true:false;
}

//验证电话号码

function fun_phone($str)
{
return (preg_match(“/^(((\d{3}))|(\d{3}-))?((0\d{2,3})|0\d{2,3}-)?[1-9]\d{6,7}$/“,$str))?true:false;
}

//验证邮编

function fun_zip($str)
{
return (preg_match(“/^[1-9]\d{5}$/“,$str))?true:false;
}

//验证url地址

function funurl($str)
{
return (preg_match(“/^http:\/\/[A-Za-z0-9]+.[A-Za-z0-9]+[\/=\?%-&~`@[]\’:+!]([^<>\”\”])$/“,$str))?true:false;

}
9、simplexml_load_string函数的使用
输入:
<?php
$note=<<

Tove

Jani

Reminder

undefined

XML;

$xml=simplexml_load_string($note);
print_r($xml);
?>

输出:
SimpleXMLElement Object ( [to] => Tove
[from] => Jani
[heading] => Reminder
[body] => Don’t forget me this weekend! )