PHP基于PDO的 mysql 查询工具
单页面实现,将页面放在任意目录即可。
访问用户 admin 密码 password
代码很简单,主要为了在没有phpMyAdmin时方便执行SQL。
效果如下:
代码如下:
<?php // 权限验证
$user_list = [
'admin' => 'password',
]; $user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW']; if (empty($user_list[$user]) || $user_list[$user] != $pass)
{
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
die ("Not authorized");
} // 参数
$row_limit = @$_GET['row_limit'] ?: 1000; // 显示行数限制
$str_limit = @$_GET['str_limit'] ?: 100; // 显示值长度限制
$db_config = [
'connect' => @$_POST['db_connect'] ?: '',
'user' => @$_POST['db_user'] ?: '',
'password' => @$_POST['db_password'] ?: '',
]; $sql = @$_POST['sql'] ?: ''; // 查询SQL
function runSql($db, $sql)
{
$dt_begin = round(microtime(true) * 1000); $con = new PDO($db['connect'], $db['user'], $db['password'], [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES `utf8`',
PDO::ATTR_PERSISTENT => FALSE,
]); $con -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con -> setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); $sql = trim($sql); $sql_type = strtoupper(explode(' ', $sql)[0]); if (strpos($sql, 'INTO OUTFILE') !== FALSE)
{
$sql_type = 'OUTFILE';
} $cmd = $con -> prepare($sql); $cmd -> execute(); $return = NULL;
if ($sql_type == 'SELECT' || $sql_type == 'SHOW' || $sql_type == 'DESC')
{
$return = $cmd -> fetchAll(PDO::FETCH_ASSOC);
}
else if ($sql_type == 'INSERT')
{
$return = $con -> lastInsertId();
}
else
{
$return = $cmd -> rowCount();
} $dt_begin = round(microtime(true) * 1000) - $dt_begin;
$res = [
'use_time' => $dt_begin,
'data' => $return,
]; return $res;
} // 执行SQL
$res = null;
$error = '';
if (!empty($sql))
{
try
{
$res = runSql($db_config, $sql);
}
catch(Exception $ex)
{
$error = $ex;
}
} // 格式化值输出
function f($v)
{
global $str_limit; $row_count = count(explode("\n", $v)); $v = htmlspecialchars($v);
$v = nl2br($v); if (strlen($v) > $str_limit)
{
$v = substr($v, 0, $str_limit) . '...';
} if ($row_count > 1)
{
$v = '<div style="text-align:left;">' . $v . '</div>';
} return $v;
} ?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>DB 查询工具</title>
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<style>
body{
line-height:1.5;
font-family:consolas, Courier New;
}
input[type=text], input[type=password], textarea{
font-family:consolas, Courier New;
padding:3px;
}
</style>
</head>
<body>
<h1 style="margin:5px 0; font-size:16px;">MySql 查询工具</h1>
<form id="frmQuery" method="POST">
<input type="text" name="db_connect" style="width:500px;" value="<?=$db_config['connect'] ?>" placeholder="连接字符串" />
<input type="text" name="db_user" style="width:100px;" value="<?=$db_config['user'] ?>" placeholder="用户名" />
<input type="password" name="db_password" style="width:100px;" value="<?=$db_config['password'] ?>" placeholder="密码" /><br>
<div style="font-size:12px; color:gray;">例如:mysql:host=localhost;port=3306;dbname=test</div>
<textarea name="sql" placeholder="SQL语句" style="color:blue; width:740px; height:150px; font-size:14px;"><?=$sql ?></textarea>
<div>
<button type="submit">执行</button>
 
<button type="button" onclick="runSql('show tables;'); return false;">所有表</button>
<button type="button" onclick="runSql('show table status;'); return false;">表详情</button>
<button type="button" onclick="runSql('show full processlist;'); return false;">进程</button>
<button type="button" onclick="runSql('show databases;'); return false;">数据库</button>
<button type="button" onclick="runSql('show status;'); return false;">状态</button>
<button type="button" onclick="runSql('show errors;'); return false;">错误</button>
<button type="button" onclick="runSql('show warnings;'); return false;">警告</button>
<button type="button" onclick="runSql('show create table table1;'); return false;">表创建</button>
<button type="button" onclick="runSql('desc table1;'); return false;">表字段</button>
<button type="button" onclick="runSql('show variables;'); return false;">配置</button>
</div>
</form>
<hr>
<?php if (!empty($error)) { ?>
<pre style="color:red; font-size:14px; font-family:consolas, Courier New;"><?= $error ?></pre>
<?php } ?>
<?php if (!empty($res)) { ?>
<span>执行用时:<?= $res['use_time']; ?>ms</span>
<?php if (is_array($res['data'])) { ?>
<style>
.table{
border-spacing: 0;
border-collapse: collapse;
border:solid 2px #aaa;
background:#fff;
font-size:12px;
}
.table th{
font-weight:normal;
}
.table td, .table th{
border:solid 1px #aaa;
padding:3px 5px;
text-align:center;
white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
}
.table thead{border-bottom:solid 2px #aaa; background:#ddd;}
.table tbody tr:hover{background:#eff;}
</style>
<span>共<?= count($res['data']); ?>行</span>
<?php if (count($res['data']) > 0) { ?>
<span>共<?= count(array_keys($res['data'][0])); ?>列</span>
<table class="table">
<thead>
<tr>
<th>#</th>
<?php foreach (array_keys($res['data'][0]) as $r) { ?>
<th><?= f($r); ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($res['data'] as $r) {
$i++;
if ($i > $row_limit)
{
break;
}
?>
<tr>
<td><?= $i; ?></td>
<?php foreach ($r as $rr) { ?>
<td><?= f($rr); ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<?php }else{ ?>
<span>影响行数:<?= $res['data']; ?></span>
<?php } ?>
<?php } ?> <script>
function runSql(sql)
{
$('textarea[name=sql]').val(sql);
$('#frmQuery').submit();
} $(function(){
$('#frmQuery').submit(function(){
if (window.localStorage)
{
window.localStorage.setItem('db_connect', $('input[name=db_connect]').val());
window.localStorage.setItem('db_user', $('input[name=db_user]').val());
window.localStorage.setItem('db_password', $('input[name=db_password]').val());
window.localStorage.setItem('db_sql', $('textarea[name=sql]').val());
}
}); if (window.localStorage)
{
var v = null;
v = window.localStorage.getItem('db_connect');
$('input[name=db_connect]').val(v||'');
v = window.localStorage.getItem('db_user');
$('input[name=db_user]').val(v||'');
v = window.localStorage.getItem('db_password');
$('input[name=db_password]').val(v||'');
v = window.localStorage.getItem('db_sql');
$('textarea[name=sql]').val(v||'');
}
});
</script>
</body>
</html>