如果我在 MySQL 客户端中进行选择,我将得到如下所示的输出:
mysql> select * FROM `group` LIMIT 2;
+----------+---------------------+-----------------+-------------+
| group_id | group_supergroup_id | group_deletable | group_label |
+----------+---------------------+-----------------+-------------+
| 1 | 4 | 0 | defaut |
| 8 | 1 | 1 | dbdfg |
+----------+---------------------+-----------------+-------------+
如何转换 PDOfetch
(or fetchAll
)这样的表中的数组?
这是一个代码使用示例:
$prep = $pdo->query('SELECT * FROM `group` LIMIT 2;');
$arr = $prep->fetchAll(PDO::FETCH_ASSOC);
echo renderMySQLTable($arr);
接近于什么的东西mysql客户端输出:
$data = array(
array(
'group_id' => '1',
'group_supergroup_id' => '4',
'group_deletable' => '0',
'group_label' => 'default',
),
array(
'group_id' => '8',
'group_supergroup_id' => '1',
'group_deletable' => '1',
'group_label' => 'dbdfg',
),
);
if ( empty($data) ) {
echo "Empty set";
} else {
// determine widths of titles
$colWidths = array();
foreach ( $data[0] as $title => $value ) {
$colWidths[$title] = strlen($title);
}
// determine widths of columns
foreach ( $data as $row ) {
foreach ( $row as $title => $value ) {
if ( is_null($value) ) {
$value = 'NULL';
}
if ( $colWidths[$title] < strlen($value) ) {
$colWidths[$title] = strlen($value);
}
}
}
// generate horizontal border
$horizontalBorder = '+';
foreach ( $colWidths as $title => $width ) {
$horizontalBorder .= str_repeat('-', $width + 2) . "+";
}
$horizontalBorder .= "\n";
// print titles
echo $horizontalBorder;
echo '|';
foreach ( $data[0] as $title => $value ) {
printf(" %-{$colWidths[$title]}s |", $title);
}
echo "\n";
echo $horizontalBorder;
// print contents
foreach ( $data as $row ) {
echo "|";
foreach ( $row as $title => $value ) {
if ( is_null($value) ) {
$value = 'NULL';
}
printf(" %-{$colWidths[$title]}s |", $value);
}
echo "\n";
}
echo $horizontalBorder;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)