0

这是一个包含列和值的示例:

+-------------------------------------------------- ---------------+
| 率 | 连接/秒 | 请求/秒 | 回复/s 平均 | 错误 | 净 io (KB/s) |
+-------------------------------------------------- ---------------+
| 100 | 99.9 | 99.9 | 99.7 | 0 | 45.4 |
| 120 | 119.7 | 119.7 | 120.0 | 0 | 54.4 |
| 140 | 139.3 | 139.3 | 138.0 | 0 | 63.6 |
|> 160 | 151.9 | 151.9 | 147.0 | 0 | 69.3 |
| 180 | 132.2 | 129.8 | 137.4 | 27 | 59.6 |
| 200 | 119.8 | 117.6 | 139.9 | 31 | 53.9 |
+-------------------------------------------------- ---------------+
4

3 回答 3

1

假设您想在浏览器中显示它并从 MySQL 获取数据。使用此代码,将您的dbnameand替换tablename为正确的值:

<html>
<head><title>Result as Table</title></head>
<body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'dbname';
$table = 'tablename';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
于 2010-11-28T05:18:15.007 回答
0

查看printf/sprintf

printf('| %3s | %-5.1f | …', $rate, $conns, …);
于 2010-11-28T05:17:45.250 回答
0

printf() 是你的朋友。

<?php
function printRows($rows){
    $br = "+----------------------------------------------------------------+";
    $he = "| rate | conn/s | req/s | replies/s avg | errors | net io (KB/s) |";

    //Print out the header
    print "$br\n$he\n$br\n";

    //Iterate through the rows
    foreach($rows as $row){
        printf("|[%-5s] | [%-7s] | [%-5s] | [%-13s] | [%-6s] | [%-13s] |",
               $row[0],$row[1],$row[2],$row[3],$row[4],$row[5] 
              );

    }

    //Print the end ascii-art
    print "$br\n";
}
?>
于 2010-11-28T05:18:14.713 回答