首页 > 代码库 > tp5 r3 一个简单的SQL语句调试实例

tp5 r3 一个简单的SQL语句调试实例

tp5 r3 一个简单的SQL语句调试实例
先看效果
技术分享
核心代码

  1.     public function index()
  2.     {
  3.         if (IS_AJAX && session("uid")==1){
  4.             $sql=input("post.sql");
  5.             $sql=base64_decode($sql);
  6.             if ($sql){
  7.                 if (preg_match(‘/INSERT INTO|DELETE|UPDATE\s/i‘, $sql, $info)){
  8.                     $result=\think\Db::execute($sql);
  9.                 }
  10.                 else{                    
  11.                     $result=\think\Db::query($sql);
  12.                 }
  13.                 return dump($result);//json_encode($result)
  14.             }else{
  15.                 return  "错误的sql语句"; 
  16.             }
  17.             
  18.         }
  19.         //读取所有的表
  20.         $tables=\think\Db::query(‘SHOW TABLES‘);
  21.         $this->assign(‘tables‘,$tables);
  22.         $database=\think\Config::get("database");
  23.         $this->assign(‘tkey‘,"tables_in_".$database["database"]);
  24.         return $this->fetch();
  25.          
  26.     }
复制代码

前台

    1. {include file="Public:header" /}
    2. <div id="page-content" style="min-height: 395px;">
    3.     <div class="row">
    4.         <div class="col-md-6">
    5.            <div class="block full">
    6.                 <div class="block-title"><h2>数据库命令</h2></div>
    7.                     <form id="form-sql" onsubmit="return!1" class="form-horizontal form-bordered" method="post" action="{:url(‘db/index?type=save‘)}">
    8.                         <div class="form-group">
    9.                             <div class="col-md-9 col-md-offset-3">
    10.                                 
    11.                                 <div class="col-xs-6">
    12.                                     <select size="1" class="form-control" id="tables">
    13.                                         {volist name="tables" id="vo"}
    14.                                             <option value="{$vo[$tkey]}">{$vo[$tkey]}</option>
    15.                                         {/volist}
    16.                                     </select>
    17.                                 </div>
    18.                                 <div class="btn-group">                                     
    19.                                     <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)" aria-expanded="false">快捷SQL语句<span class="caret"></span></a>
    20.                                     <ul class="dropdown-menu text-left">
    21.                                         <li class="dropdown-header"><class="fa fa-user pull-right"></i><strong>select语句</strong></li>
    22.                                         <li><a href="javascript:insert(‘SELECT * FROM `[table]` WHERE 1‘)">select</a></li>
    23.                                         <li><a href="javascript:insert(‘INSERT INTO `[table]` VALUES ([value-1],[value-2],[value-3],[value-4],[value-5])‘)">insert</a></li>
    24.                                         <li><a href="javascript:insert(‘UPDATE `[table]` SET `[]`=[value-2] WHERE 1‘)">update</a></li>
    25.                                         <li><a href="javascript:insert(‘DELETE FROM `[table]` WHERE 1‘)">delete</a></li>                                        
    26.                                         <li class="divider"></li>                                        
    27.                                     </ul>
    28.                                 </div>
    29.                                 
    30.                             </div>
    31.                         </div>
    32.                         <div class="form-group">
    33.                             <label for="example-textarea-input" class="col-md-3 control-label">SQL命令语句</label>
    34.                             <div class="col-md-9">
    35.                                 <textarea placeholder="SQL命令语句" class="form-control" rows="7" name="sql" id="sql"></textarea>
    36.                             </div>
    37.                         </div>
    38.                         <div class="form-group form-actions">
    39.                             <div class="col-md-9 col-md-offset-3">
    40.                                 <button class="btn btn-effect-ripple btn-primary" id="form" type="button" style="overflow:hidden;position:relative">提交</button>
    41.                                 <button class="btn btn-effect-ripple btn-danger" type="reset" style="overflow:hidden;position:relative">重置</button>
    42.                             </div>
    43.                         </div>
    44.                     </form>
    45.            </div> 
    46.         </div>
    47.         <div class="col-md-6">
    48.             <div class="block full">
    49.                 <div class="block-title"><h2>输出结果</h2></div>
    50.                 <p id="result" style="#cccccc;"></p>
    51.            </div>
    52.         </div>
    53.     </div>
    54.     
    55. </div>
    56. {include file="Public:footer" /}
    57. <script src="__PUBLIC__/static/js/base64/base64.js"></script>
    58. <script>
    59. function insert($sql){
    60.     if ($sql){
    61.         var table=$("#tables").val();
    62.         $("#sql").text($sql.replace("[table]", table));
    63.     }
    64. }
    65. $("#form").click(function(){
    66.     var sql=$("#sql").val();
    67.     var url =$("#form-sql").attr("action");
    68.     if (!sql){jError("SQL不能为空!!", {VerticalPosition: ‘center‘,HorizontalPosition: ‘center‘});return;}
    69.     sql=base64_encode(sql);
    70.     $.ajax({  
    71.          url:url,
    72.          data:{sql:sql},  
    73.          type:‘post‘,  
    74.          cache:true,  
    75.          dataType:‘html‘,  
    76.          success:function(data) {  
    77.             $("#result").html(data); 
    78.           },  
    79.           error : function() {  
    80.               $("#result").html(‘错误的SQL语句!<br/>‘+$("#sql").val());
    81.               jError("错误的SQL语句!", {VerticalPosition: ‘center‘,HorizontalPosition: ‘center‘});
    82.           }  
    83.      }); 
    84.       
    85.     
    86. })
    87. </script>

tp5 r3 一个简单的SQL语句调试实例