将 SQL 数据库中的数据显示到 php/html 表中

新手上路,请多包涵

我有一个 MySQL 数据库,我想在 HTML 或 PHP 表上显示我的一个 SQL 表。我在网上搜索过,无法实现这个功能。有人可以帮我编码吗?

 database = 'hrmwaitrose'
username = 'root'
host = 'localhost'

没有密码。

我想显示“员工”表中的数据。

原文由 user2108411 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 282
2 个回答

PHP 提供了连接到 MySQL 数据库的函数。

 $connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('hrmwaitrose');

$query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL
$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr><td>" . htmlspecialchars($row['name']) . "</td><td>" . htmlspecialchars($row['age']) . "</td></tr>";  //$row['index'] the index here is a field name
}

echo "</table>"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection

在 while 循环(每次遇到结果行时运行)中,我们回显创建一个新的表行。我还添加了一个包含字段。

这是一个非常基本的模板。您会看到使用 mysqli_connect 而不是 mysql_connect 的其他答案。 mysqli 代表改进的 mysql。它提供了更好的功能范围。你注意到它也有点复杂。这取决于你需要什么。

请注意,自 PHP 5.5.0 起,“mysql_fetch_array”现已弃用,并在 PHP 7.0.0 中删除。所以请改为查看“mysqli_fetch_array()”。

原文由 Jacob Valenta 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是我编写的一个简单函数,用于显示表格数据而无需输入每个列名:(另外,请注意:嵌套循环)

 function display_data($data) {
    $output = '<table>';
    foreach($data as $key => $var) {
        $output .= '<tr>';
        foreach($var as $k => $v) {
            if ($key === 0) {
                $output .= '<td><strong>' . $k . '</strong></td>';
            } else {
                $output .= '<td>' . $v . '</td>';
            }
        }
        $output .= '</tr>';
    }
    $output .= '</table>';
    echo $output;
}


更新功能如下

嗨,杰克,

你的函数设计很好,但这个函数总是错过数组中的第一个数据集。我对此进行了测试。

你的功能非常好,很多人会用它,但他们总是会错过第一个数据集。这就是我写这个修正案的原因。

缺少的数据集是由条件 if key === 0 导致的。如果 key = 0,则仅写入列标题,但不写入包含 $key 0 的数据。所以总是缺少数组的第一个数据集。

您可以通过将 if 条件移动到第二个 foreach 循环之上来避免这种情况,如下所示:

 function display_data($data) {
    $output = "<table>";
    foreach($data as $key => $var) {
        //$output .= '<tr>';
        if($key===0) {
            $output .= '<tr>';
            foreach($var as $col => $val) {
                $output .= "<td>" . $col . '</td>';
            }
            $output .= '</tr>';
            foreach($var as $col => $val) {
                $output .= '<td>' . $val . '</td>';
            }
            $output .= '</tr>';
        }
        else {
            $output .= '<tr>';
            foreach($var as $col => $val) {
                $output .= '<td>' . $val . '</td>';
            }
            $output .= '</tr>';
        }
    }
    $output .= '</table>';
    echo $output;
}

最诚挚的问候和感谢 - Axel Arnold Bangert - Herzogenrath 2016

另一个更新删除了损害代码可维护性的冗余代码块。

 function display_data($data) {
$output = '<table>';
foreach($data as $key => $var) {
    $output .= '<tr>';
    foreach($var as $k => $v) {
        if ($key === 0) {
            $output .= '<td><strong>' . $k . '</strong></td>';
        } else {
            $output .= '<td>' . $v . '</td>';
        }
    }
    $output .= '</tr>';
}
$output .= '</table>';
echo $output;

}

原文由 Jack 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题