Php 如何使用html表显示数据库中的数据
我试图在HTML表中显示数据库中的数据。主要问题是:该表没有出现Php 如何使用html表显示数据库中的数据,php,html,pdo,Php,Html,Pdo,我试图在HTML表中显示数据库中的数据。主要问题是:该表没有出现 <html> <head> <body> <table> <tr> <th>Id</th> <th>String</th> <th>Array</th> </tr> <?php // Mostrar dados echo "<table>"; {
<html>
<head>
<body>
<table>
<tr>
<th>Id</th>
<th>String</th>
<th>Array</th>
</tr>
<?php
// Mostrar dados
echo "<table>";
{
$databaseName = 'visteon';
$pdo = new Pdo('mysql:host=127.0.0.1;dbname=' . $databaseName, 'root', '');
$result = $pdo->query('SHOW TABLES FROM ' . $databaseName)->fetchAll(PDO::FETCH_NUM);
$Tabela = [];
foreach ($result as $r) {
$Tabela[] = $r[0];
}
$data = [];
foreach ($Tabela as $Tabela) {
$data[$Tabela] = $pdo->query('SELECT * FROM ' . $Tabela)->fetchAll(PDO::FETCH_ASSOC);
}
echo "</table>";
var_dump($Tabela);
var_dump($data);
}
?>
</table>
</body>
</html>
您的代码中缺少完整的内部表结构,而且您没有向HTML表输出数据,只是创建本地PHP变量并在生成表后转储它们的值。在代码中使用try-catch,如下所示:
try {
$pdo = new Pdo('mysql:host=127.0.0.1;dbname=$databaseName .', 'root',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$result = $pdo->query('SHOW TABLES FROM ' . $databaseName)->fetchAll(PDO::FETCH_NUM);
} catch (PDOException $e) {
throw new Exception("Error with PDO: " . $e->getMessage());
}
并更正代码检查我的第二行。您需要添加到。您的SQL查询代码易受SQL注入代码的攻击。您应该将Prepared语句与@yunzen一起使用,因为没有使用用户输入,所以代码不允许SQL注入。此外,您无法准备数据库或表名。默认情况下,PDO不会引发任何异常@DarkBee所说的内容,而且打印Oh noes也不是一种好方法。请查看PHP错误报告规则,