如何在浏览器中查询mysql数据并以ascii文本表的形式显示它,就像在命令行中获取它一样。 下面举一个小例子:
+------+---------+---------------------+------------+
| S.No | S.R.NO. | EMPLOYEE NAME | D.O.B |
+------+---------+---------------------+------------+
| 1 | 0 | T CHANDRASHEKAR | 01/01/2000 |
| 2 | 000102 | A RAMESH | 01/01/2000 |
| 3 | 601026 | B DEEPAK KUMAR | 01/01/2000 |
| 4 | 543250 | N VIRUPAKSHAIAH | 02/01/2000 |
| 5 | 610019 | ANAND S GUNDI | 15/01/2000 |
| 6 | 535035 | ADAM BASHA | 27/01/2000 |
| 7 | 625893 | A N MOHAN KUMAR | 06/02/2000 |
| 8 | 547830 | RAJENDRA NAIK | 07/02/2000 |
| 9 | 698742 | S M SUSHMA | 19/02/2000 |
| 10 | 655542 | B JAYACHANDRA | 24/02/2000 |
| 11 | 624769 | B HANUMANTHAPPA | 25/02/2000 |
如果可以使用 php 会更好。 请帮忙。
方法
这是可能的。主要困难是字符串长度的可变性,这通常会导致输出数据难以读取。
为了解决这个问题,我建议你:
- 查找每列中的最大字符串长度
- 具有上边框和下边框的输出标题,标题名称填充为与最大单元格宽度相同的宽度
- 输出每一行,并将单元格填充到目标列宽。
代码示例
我目前无法对下面显示的代码进行单元测试,但应该没问题;如果您有任何问题,请发表评论。
<?php
// Open connection
$mysqli = new mysqli("db_host", "db_user", "db_password", "db_schema");
// Check that connection succeeded
if ($mysqli->connect_errno) {
printf("Connect failed: %s'n", $mysqli->connect_error);
exit();
}
// Prepare query
$query = "SELECT * FROM `myTable`";
// Fetch results as associative array
$resultSet = [];
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$resultSet[] = $row;
}
// Free result set
$result->free();
// Close Connection to DB
$mysqli->close();
} else {
// Close Connection to DB and print error message
$mysqli->close();
die('An error occurred when executing SQL, please check your SQL query.');
}
// If results empty, output error
if (count($resultSet)<1) {
die('<pre>----NO DATA----'+"'n'n"+'Please revise your query</pre>');
}
// Get column names
$keys = array_keys($resultSet[0]);
// Iterate over column names, get starting widths
$maxLength = [];
foreach ($keys as $column) {
$maxLength[$column] = mb_strlen($column);
}
// Iterate over result-set, get maximum string length for each column
foreach ($resultSet as $row) {
foreach ($keys as $column) {
// If current cell length is greater than column width, increase column width
if (mb_strlen($row[$column]) > $maxLength[$column]) {
$maxLength[$column] = mb_strlen($row[$column]);
}
}
}
echo '<pre>';
// Output top border
echo '+';
foreach ($keys as $column) {
echo str_repeat('-', $maxLength[$column]+2);
echo '+';
}
echo "'n";
// Output header
echo '| ';
foreach ($keys as $column) {
echo $column.str_repeat(' ', $maxLength[$column] - mb_strlen($column));
echo ' | ';
}
echo "'n";
// Output bottom border
echo '+';
foreach ($keys as $column) {
echo str_repeat('-', $maxLength[$column]+2);
echo '+';
}
echo "'n";
// Output all rows
foreach ($resultSet as $row) {
echo '| ';
foreach ($keys as $column) {
echo $row[$column].str_repeat(' ', $maxLength[$column] - mb_strlen($row[$column]));
echo ' | ';
}
echo "'n";
}
echo '</pre>';
?>
注意上面的代码是多字节安全的,但它忽略了字符宽度;如果输出包含可变宽度的字符,那么列分隔符中可能会有轻微的"扭结"。如果您在显示器中遇到此类问题,这可能值得修改。
编辑:如果不编写花哨的包装脚本来模拟确切的MySQL shell输出(例如@PeterScott的答案),看起来是不可能的。您可以获得的最接近的可能是将输出转储到临时文件中:
<?php
mysqli_query("select * from mytable into outfile '/tmp/mytable.out'");
然后在浏览器中输出此文件,将其包装在 PRE 标记中:
<?php
$output = file_get_contents('/tmp/mytable.out');
echo '<pre>'. $output .'</pre>';
恐怕无法直接从MySQL控制台获取这些数据。
mysql -uroot -p dbname -e 'select * from table_name' -H > dump.php
然后您可以从浏览器访问表格内容:http://192.168.2.xx/dump.php
-h在mysql命令行中表示HTML格式的输出。