PHP的PDO能不能打印出上一条执行的sql语句
1个回答
展开全部
打印sql语句,直接在你执行SQL语句后输出
$queries = DB::getQueryLog();
$a = end($queries);
$tmp = str_replace('?', '"'.'%s'.'"', $a["query"]);
echo vsprintf($tmp, $a['bindings']);
exit;
实例:
<?php
Route::get('/', function()
{
$arr['name']='zhuo';
$arr['email']='zhuowenji@163.com';
$uid = DB::table('basic')->insertGetId($arr);
$queries = DB::getQueryLog();
/*
echo '<pre>';
var_dump($queries);
echo '</pre>';
//以下为得到结果。laravel默认方式使用了pdo的形式执行对数据库操作
array(1) {
[0]=>
array(3) {
["query"]=>
string(51) "insert into `basic` (`name`, `email`) values (?, ?)"
["bindings"]=>
array(2) {
[0]=>
string(4) "zhuo"
[1]=>
string(17) "zhuowenji@163.com"
}
["time"]=>
float(2)
}
}
*/
//===========================================================
//转成源生的sql语句
if($uid == false)
{
$a = end($queries);
$tmp = str_replace('?', '"'.'%s'.'"', $a["query"]);
echo vsprintf($tmp, $a['bindings']);exit;
//结果;insert into `basic` (`name`, `email`) values ("zhuo", "zhuowenji@163.com")
}
});
?>
$queries = DB::getQueryLog();
$a = end($queries);
$tmp = str_replace('?', '"'.'%s'.'"', $a["query"]);
echo vsprintf($tmp, $a['bindings']);
exit;
实例:
<?php
Route::get('/', function()
{
$arr['name']='zhuo';
$arr['email']='zhuowenji@163.com';
$uid = DB::table('basic')->insertGetId($arr);
$queries = DB::getQueryLog();
/*
echo '<pre>';
var_dump($queries);
echo '</pre>';
//以下为得到结果。laravel默认方式使用了pdo的形式执行对数据库操作
array(1) {
[0]=>
array(3) {
["query"]=>
string(51) "insert into `basic` (`name`, `email`) values (?, ?)"
["bindings"]=>
array(2) {
[0]=>
string(4) "zhuo"
[1]=>
string(17) "zhuowenji@163.com"
}
["time"]=>
float(2)
}
}
*/
//===========================================================
//转成源生的sql语句
if($uid == false)
{
$a = end($queries);
$tmp = str_replace('?', '"'.'%s'.'"', $a["query"]);
echo vsprintf($tmp, $a['bindings']);exit;
//结果;insert into `basic` (`name`, `email`) values ("zhuo", "zhuowenji@163.com")
}
});
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询