PHP 函数在 HTML 中打印 table
PHP function to print table in HTML
使用PHP生成HTML代码来显示table(这是一个等长数组的数组)相当容易,例如下面的代码。但是我想知道:是否有 PHP 函数可以为您执行此操作?我在print table in php
上搜索了Google,在table
上搜索了PHP手册,但没有找到这样的功能。
来自 http://davidwalsh.name/html-mysql-php 的代码打印 table:
$result2 = mysql_query('SHOW COLUMNS FROM '.$table) or die('cannot show columns from '.$table);
if(mysql_num_rows($result2)) {
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>';
while($row2 = mysql_fetch_row($result2)) {
echo '<tr>';
foreach($row2 as $key=>$value) {
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
没有这样的内置函数。您创建了自己的,这就是它的工作原理。总有 var_dump(),但普通访问者无法阅读。
回答你的问题:没有,没有。
但是,我确实发现这个想法很有趣,所以我编写了一个函数来实现这一点。但是,您的示例使用 MySQL() 已弃用且不安全。所以我将改用 PDO class。
请记住,此功能仅对打印数据库有用 table。它不能防止注入,因此永远不应该在查询中与用户输入一起使用!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "pdo.class.php";
//Database data
define("DB_HOST", "localhost");
define("DB_USER", "");
define("DB_PASS", "");
define("DB_NAME", "");
function printTable($tbl_name, $db_query){
//New PDO object
$pdo = new Database();
//Get column names
$pdo->query("DESCRIBE ". $tbl_name);
$col_names = $pdo->column();
//Get number of columns
$col_cnt = count($col_names);
//Setup table - user css class db-table for design
echo "<table class='db-table'>";
echo "<tr colspan='". $col_cnt ."'>". $tbl_name ."</tr>";
echo "<tr>";
//Give each table column same name is db column name
for($i=0;$i<$col_cnt;$i++){
echo "<td>". $col_names[$i] ."</td>";
}
echo "</tr>";
//Get db table data
$pdo->query($db_query);
$results = $pdo->resultset();
$res_cnt = count($results);
//Print out db table data
for($i=0;$i<$res_cnt;$i++){
echo "<tr>";
for($y=0;$y<$col_cnt;$y++){
echo "<td>". $results[$i][$col_names[$y]] ."</td>";
}
echo "</tr>";
}
}
//Query
$sqlq = "SELECT * FROM tablename";
//Useage: printTable("tablename","query");
printTable("tablename",$sqlq);
?>
PDO class 本身:
Class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function column(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_COLUMN);
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
使用PHP生成HTML代码来显示table(这是一个等长数组的数组)相当容易,例如下面的代码。但是我想知道:是否有 PHP 函数可以为您执行此操作?我在print table in php
上搜索了Google,在table
上搜索了PHP手册,但没有找到这样的功能。
来自 http://davidwalsh.name/html-mysql-php 的代码打印 table:
$result2 = mysql_query('SHOW COLUMNS FROM '.$table) or die('cannot show columns from '.$table);
if(mysql_num_rows($result2)) {
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>';
while($row2 = mysql_fetch_row($result2)) {
echo '<tr>';
foreach($row2 as $key=>$value) {
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
没有这样的内置函数。您创建了自己的,这就是它的工作原理。总有 var_dump(),但普通访问者无法阅读。
回答你的问题:没有,没有。
但是,我确实发现这个想法很有趣,所以我编写了一个函数来实现这一点。但是,您的示例使用 MySQL() 已弃用且不安全。所以我将改用 PDO class。
请记住,此功能仅对打印数据库有用 table。它不能防止注入,因此永远不应该在查询中与用户输入一起使用!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "pdo.class.php";
//Database data
define("DB_HOST", "localhost");
define("DB_USER", "");
define("DB_PASS", "");
define("DB_NAME", "");
function printTable($tbl_name, $db_query){
//New PDO object
$pdo = new Database();
//Get column names
$pdo->query("DESCRIBE ". $tbl_name);
$col_names = $pdo->column();
//Get number of columns
$col_cnt = count($col_names);
//Setup table - user css class db-table for design
echo "<table class='db-table'>";
echo "<tr colspan='". $col_cnt ."'>". $tbl_name ."</tr>";
echo "<tr>";
//Give each table column same name is db column name
for($i=0;$i<$col_cnt;$i++){
echo "<td>". $col_names[$i] ."</td>";
}
echo "</tr>";
//Get db table data
$pdo->query($db_query);
$results = $pdo->resultset();
$res_cnt = count($results);
//Print out db table data
for($i=0;$i<$res_cnt;$i++){
echo "<tr>";
for($y=0;$y<$col_cnt;$y++){
echo "<td>". $results[$i][$col_names[$y]] ."</td>";
}
echo "</tr>";
}
}
//Query
$sqlq = "SELECT * FROM tablename";
//Useage: printTable("tablename","query");
printTable("tablename",$sqlq);
?>
PDO class 本身:
Class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function column(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_COLUMN);
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}