tp5 r3 一个简单的SQL语句调试实例
先看效果
核心代码 public function index()<br>
{<br>
if (IS_AJAX && session("uid")==1){<br>
$sql=input("post.sql");<br>
$sql=base64_decode($sql);<br>
if ($sql){<br>
if (preg_match('/INSERT INTO|DELETE|UPDATE\s/i', $sql, $info)){<br>
$result=\think\Db::execute($sql);<br>
}<br>
else{ <br>
$result=\think\Db::query($sql);<br>
}<br>
return dump($result);//json_encode($result)<br>
}else{<br>
return "错误的sql语句"; <br>
}<br>
<br>
}<br>
//读取所有的表<br>
$tables=\think\Db::query('SHOW TABLES');<br>
$this->assign('tables',$tables);<br>
$database=\think\Config::get("database");<br>
$this->assign('tkey',"tables_in_".$database["database"]);<br>
return $this->fetch();<br>
<br>
}
前台{include file="Public:header" /}<br>
<div>
<br>
<div>
<br>
<div>
<br>
<div>
<br>
<div><h2>数据库命令</h2></div>
<br>
<form>
<br>
<div>
<br>
<div>
<br>
<br>
<div>
<br>
<select><br>
{volist name="tables" id="vo"}<br>
<option>{$vo[$tkey]}</option>
<br>
{/volist}<br>
</select><br>
</div>
<br>
<div> <br>
<a>快捷SQL语句<span></span></a><br>
<ul>
<br>
<li>
<i></i><strong>select语句</strong>
</li>
<br>
<li><a>select</a></li>
<br>
<li><a>insert</a></li>
<br>
<li><a>update</a></li>
<br>
<li><a>delete</a></li> <br>
<li> <br>
</ul>
<br>
</div>
<br>
<br>
</div>
<br>
</div>
<br>
<div>
<br>
<label>SQL命令语句</label><br>
<div>
<br>
<textarea></textarea><br>
</div>
<br>
</div>
<br>
<div>
<br>
<div>
<br>
<button>提交</button><br>
<button>重置</button><br>
</div>
<br>
</div>
<br>
</form>
<br>
</div> <br>
</div>
<br>
<div>
<br>
<div>
<br>
<div><h2>输出结果</h2></div>
<br>
<p></p>
<br>
</div>
<br>
</div>
<br>
</div>
<br>
<br>
</div>
<br>
{include file="Public:footer" /}<br>
<script></script><br>
<script><br />
function insert($sql){<br />
if ($sql){<br />
var table=$("#tables").val();<br />
$("#sql").text($sql.replace("[table]", table));<br />
}<br />
}<br />
$("#form").click(function(){<br />
var sql=$("#sql").val();<br />
var url =$("#form-sql").attr("action");<br />
if (!sql){jError("SQL不能为空!!", {VerticalPosition: 'center',HorizontalPosition: 'center'});return;}<br />
sql=base64_encode(sql);<br />
$.ajax({ <br />
url:url,<br />
data:{sql:sql}, <br />
type:'post', <br />
cache:true, <br />
dataType:'html', <br />
success:function(data) { <br />
$("#result").html(data); <br />
}, <br />
error : function() { <br />
$("#result").html('错误的SQL语句!<br/>'+$("#sql").val());<br />
jError("错误的SQL语句!", {VerticalPosition: 'center',HorizontalPosition: 'center'});<br />
} <br />
}); <br />
<br />
<br />
})<br />
</script>
AD:真正免费,域名+虚机+企业邮箱=0元