在PHP中将SELECT查询的结果打印为预格式化文本?

时间:2022-08-28 23:32:25

I'm looking for an easy and quick way to print out the results of a MySQL SELECT query in PHP as preformatted text. What I would like is to be able to pass a query object to a function and get a printout of the recordset like the command line MySQL client does when running SELECT statements.

我正在寻找一种简单而快速的方法来打印PHP中的MySQL SELECT查询作为预格式化文本的结果。我想要的是能够将查询对象传递给函数并获得记录集的打印输出,就像命令行MySQL客户端在运行SELECT语句时所做的那样。

Here is an example of how I want it to look (i.e. ASCII):

下面是我希望它看起来如何的一个例子(例如,ASCII):

+----+-------------+
| id | countryCode |
+----+-------------+
|  1 | ES          |
|  2 | AN          |
|  3 | AF          |
|  4 | AX          |
|  5 | AL          |
|  6 | DZ          |
|  7 | AS          |
|  8 | AD          |
|  9 | AO          |
| 10 | AI          |
+----+-------------+

It's basically for a generic import script, I am doing a SELECT query and want to display the results to the user for confirmation.

它基本上是一个通用的导入脚本,我正在执行一个SELECT查询,并希望将结果显示给用户以进行确认。

2 个解决方案

#1


3  

sprintf is your friend, if you must have a non-HTML fixed width output.

sprintf是您的朋友,如果您必须有一个非html固定宽度的输出。

ETA:

埃塔:

//id: integer, max width 10
//code: string max width 2

$divider=sprintf("+%-10s+%-13s+",'-','-');

$lines[]=$divider;
$lines[]=sprintf("|%10s|%13s|",'id','countryCode'); //header
$lines[]=$divider;

while($line=$records->fetch_assoc()) {
    //store the formatted output
    $lines[]=sprintf("| %10u | %2.2s |", $line['id'],$line['code']);
}
$table=implode("\n",$lines);
echo $table;

If you want to print out immediately instead of storing the results, use printf instead- same syntax. There is a reasonable PHP (s)printf tutorial here.

如果您想立即打印输出而不是存储结果,请使用printf代替-相同的语法。这里有一个合理的PHP (s)printf教程。

#2


2  

function formatResults($cols, $rows) {
    echo'<table>';
    echo '<tr>';

    foreach ($cols as $v) {
        echo '<th>' . $v['field'] . '</th>';
    }
    echo '</tr>';

    foreach ($rows as $sRow) {
        echo '<tr>';

        foreach ($sRow as $v) {
            echo "<td>$v</td>";
        }

        echo '</tr>';
    }

    echo '</table>';
}

$qry = $pdo->query('DESCRIBE table');
$cols = $qry->fetchAll(PDO::FETCH_ASSOC);

$pdo->query('SELECT * FROM table');
$rows = $qry->fetchAll(PDO::FETCH_ASSOC);

formatResults($cols, $rows);

Untested but should work.

未经考验的但应该工作。

Edit: Missed ['field'] index ;)

编辑:错过['field']索引;)

#1


3  

sprintf is your friend, if you must have a non-HTML fixed width output.

sprintf是您的朋友,如果您必须有一个非html固定宽度的输出。

ETA:

埃塔:

//id: integer, max width 10
//code: string max width 2

$divider=sprintf("+%-10s+%-13s+",'-','-');

$lines[]=$divider;
$lines[]=sprintf("|%10s|%13s|",'id','countryCode'); //header
$lines[]=$divider;

while($line=$records->fetch_assoc()) {
    //store the formatted output
    $lines[]=sprintf("| %10u | %2.2s |", $line['id'],$line['code']);
}
$table=implode("\n",$lines);
echo $table;

If you want to print out immediately instead of storing the results, use printf instead- same syntax. There is a reasonable PHP (s)printf tutorial here.

如果您想立即打印输出而不是存储结果,请使用printf代替-相同的语法。这里有一个合理的PHP (s)printf教程。

#2


2  

function formatResults($cols, $rows) {
    echo'<table>';
    echo '<tr>';

    foreach ($cols as $v) {
        echo '<th>' . $v['field'] . '</th>';
    }
    echo '</tr>';

    foreach ($rows as $sRow) {
        echo '<tr>';

        foreach ($sRow as $v) {
            echo "<td>$v</td>";
        }

        echo '</tr>';
    }

    echo '</table>';
}

$qry = $pdo->query('DESCRIBE table');
$cols = $qry->fetchAll(PDO::FETCH_ASSOC);

$pdo->query('SELECT * FROM table');
$rows = $qry->fetchAll(PDO::FETCH_ASSOC);

formatResults($cols, $rows);

Untested but should work.

未经考验的但应该工作。

Edit: Missed ['field'] index ;)

编辑:错过['field']索引;)