gpt4 book ai didi

PHP 在 HTML 表中显示 MySQL 结果

转载 作者:行者123 更新时间:2023-11-29 23:21:05 25 4
gpt4 key购买 nike

所以我有一组可用的 PHP 代码,并试图将其显示在我的网站上。我只需要它显示为两列表,第一列是机器,第二列是状态。我不介意仅使用静态 html 显示机器列或从数据库中提取此信息。感谢您的帮助。

<?php
$host = "localhost";
$user = "root";
$pass = "hello";
$db = "GYM";
$r = mysql_connect($host, $user, $pass);

if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established.\n";
}

$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Database selected.\n";
}

$query = "SELECT status FROM use_instance ORDER BY instance_id DESC LIMIT 1 ";

$rs = mysql_query($query);

if (!$rs) {
echo "Could not execute query: $query";
trigger_error(mysql_error(), E_USER_ERROR);
}

while ($row = mysql_fetch_assoc($rs)) {
echo $row['status'] ;
}
mysql_close();
?>

最佳答案

<?php
$host = "localhost";
$user = "root";
$pass = "hello";
$db = "GYM";
$r = mysql_connect($host, $user, $pass);

if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established.\n";
}

$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Database selected.\n";
}

$query = "SELECT machine, status FROM use_instance ORDER BY instance_id DESC LIMIT 1 ";

$rs = mysql_query($query);

if (!$rs) {
echo "Could not execute query: $query";
trigger_error(mysql_error(), E_USER_ERROR);
}

?>
<table>
<tr>
<th>Machine</th>
<th>Status</th>
</tr>

<?php

while ($row = mysql_fetch_assoc($rs)) {
?>
<tr>
<td><?php echo $row['machine'];?></td>
<td><?php echo $row['status'];?></td>
</tr>
<?php
}
mysql_close();
?>
</table>

关于PHP 在 HTML 表中显示 MySQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27287273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com