[英]PHP MySQL display data by id from database - freedom placement
I would like to have the freedom to place a row entry from my database wherever i' prefer in the page. 我希望能够自由地在我的数据库中放置一个行条目,无论我喜欢哪个页面。 Right now, the php code that I use is as follows (it is clean working code):
现在,我使用的PHP代码如下(它是干净的工作代码):
<html><head></head>
<body>
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
echo $row['id']; while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
?>
</body>
</html>
I call id through the browser Eg. 我通过浏览器Eg调用id。 http://site.com/getid.php?id=10 .
http://site.com/getid.php?id=10 。 But I do not have the freedom to place my row entry within my html.
但是我没有把我的行条目放在我的html中的自由。 For eg.
例如。 like this:
像这样:
<table><tr>
<td align="center">BASIC INFO 1: <?php echo $row['basic1']; ?></td>
<td align="center">BASIC INFO 2: <?php echo $row['basic2']; ?></td>
</tr></table>
I can place html within echo PHP tags but then I have to clean up my html and thats a lot of work. 我可以在echo PHP标签中放置html但是我必须清理我的html,这就是很多工作。 Retaining HTML formatting would be preferred.
保留HTML格式是首选。 Any help or guidelines on this would be much appreciated.
任何有关此的帮助或指导将非常感激。
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
//you need to retrieve every row and save to an array for later access
for($rows = array(); $tmp = mysql_fetch_array($result);)
{
$rows[] = $tmp;
}
//now you can use the $rows array where every you want e.g. with the code from Zhube
?>
....
<table><?php foreach($rows as $r):
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
By 通过
while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
you save only the last row 你只保存最后一行
Instead of having your HTML tags as part of the PHP variable, do something like this instead: 不要将HTML标记作为PHP变量的一部分,而是执行以下操作:
<table><?php foreach($row as $r):?>
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.