【发布时间】:2013-12-01 01:54:18
【问题描述】:
我不是铁杆编码员。我现在正在学习 php/htmp/mysql/css :)
所以,我用 php 进行 mysql 查询,现在我想在 html 表中显示查询结果。但我不知道,如何在 php 和 html 代码之间传递变量。
我的 php 文件现在看起来像这样:
<?php
$con=mysqli_connect("my_db_host","my_db_username","my_db_pass","my_db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM hlstats_Servers
WHERE serverId='2'");
while($row = mysqli_fetch_array($result))
{
$name=$row['name'];
$ip=$row['address'] . ":" . $row['port'];
$player=$row['act_players'] . "/" . $row[max_players];
$map=$row['act_map'];
}
mysqli_close($con);
?>
<html>
<head>
</head>
<body>
<table>
<tr>
<td>$name</td>
</tr>
</table>
<table>
<tr>
<td>Ip cím:</td>
<td>$ip</td>
</tr>
<tr>
<td>Jelenlegi pálya:</td>
<td>$map</td>
</tr>
<tr>
<td>Játékosok</td>
<td>$player</td>
</tr>
</table>
</body>
</html>
【问题讨论】:
-
不要仅仅放置变量名(例如
$name),而是使用<?php echo $name; ?>