MySQL 结果未显示在表中?

发布于 2024-10-21 11:19:34 字数 2464 浏览 4 评论 0原文

我正在自学 PHP 和 MySQL,我试图从数据库中检索一些信息并将其放入表中。

到目前为止,仅显示表列标题,并且未显示每列的信息。 PHP 文件也需要很长时间才能显示。

请您指出我的代码的问题。

<?php
mysql_connect("localhost",$username,$password);
mysql_select_db($dbname) or die("Unable to select Database");
$query = "SELECT * FROM table_1";
$result = mysql_query($query);
$numcount = mysql_num_rows($result);
echo "<h2>$numcount rows in table_1.</h2>";
mysql_close();
?>

<table border="0" cellspacing="4" cellpadding="2">
<tr>
<th><font face="Futura">Type |</font></th>
<th><font face="Futura">Name |</font></th>
<th><font face="Futura">Street |</font></th>
<th><font face="Futura">Address1 |</font></th>
<th><font face="Futura">Address2 |</font></th>
<th><font face="Futura">Town |</font></th>
<th><font face="Futura">County |</font></th>
<th><font face="Futura">Postcode |</font></th>
<th><font face="Futura">Number |</font></th>
<th><font face="Futura">Latitude,Longitude</font></th>
</tr>

<?php
$i=0;
while ($i < 843) {
$type = mysql_result($result,$i,"type");
$name = mysql_result($result,$i,"name");
$street = mysql_result($result,$i,"street");
$addr1 = mysql_result($result,$im,"address1");
$addr2 = mysql_result($result,$im,"address2");
$town = mysql_result($result,$im,"town");
$county = mysql_result($result,$im,"county");
$postcode = mysql_result($result,$im,"postcode");
$number = mysql_result($result,$im,"number");
$latlong = mysql_result($result,$im,"latlong");
}
?>

<tr>
<td><font face="Futura"><?php echo $type;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $street;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr1;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr2;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $town;?></font></td>
</tr>
<?php
$i++;
?>
<?
echo "</table>"; 
?>
</body>
</html>

I am teaching myself PHP and MySQL, and I am trying to retrieve some information from my database and put it into a table.

So far only the table column headers are showing up, and no information is showing up for each column. It is also taking ages for the PHP file to display.

Please can you point out the problem with my code.

<?php
mysql_connect("localhost",$username,$password);
mysql_select_db($dbname) or die("Unable to select Database");
$query = "SELECT * FROM table_1";
$result = mysql_query($query);
$numcount = mysql_num_rows($result);
echo "<h2>$numcount rows in table_1.</h2>";
mysql_close();
?>

<table border="0" cellspacing="4" cellpadding="2">
<tr>
<th><font face="Futura">Type |</font></th>
<th><font face="Futura">Name |</font></th>
<th><font face="Futura">Street |</font></th>
<th><font face="Futura">Address1 |</font></th>
<th><font face="Futura">Address2 |</font></th>
<th><font face="Futura">Town |</font></th>
<th><font face="Futura">County |</font></th>
<th><font face="Futura">Postcode |</font></th>
<th><font face="Futura">Number |</font></th>
<th><font face="Futura">Latitude,Longitude</font></th>
</tr>

<?php
$i=0;
while ($i < 843) {
$type = mysql_result($result,$i,"type");
$name = mysql_result($result,$i,"name");
$street = mysql_result($result,$i,"street");
$addr1 = mysql_result($result,$im,"address1");
$addr2 = mysql_result($result,$im,"address2");
$town = mysql_result($result,$im,"town");
$county = mysql_result($result,$im,"county");
$postcode = mysql_result($result,$im,"postcode");
$number = mysql_result($result,$im,"number");
$latlong = mysql_result($result,$im,"latlong");
}
?>

<tr>
<td><font face="Futura"><?php echo $type;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $street;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr1;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr2;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $town;?></font></td>
</tr>
<?php
$i++;
?>
<?
echo "</table>"; 
?>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-10-28 11:19:34
  1. 当你之前执行 mysql_close() 时 mysql_result() 有效吗?

  2. 为什么不在这里使用mysql_fetch_row()?

编辑@XcodeDev的更多信息:

当您需要单个结果时,您可以使用mysql_result(),例如:

$query = mysql_query("SELECT COUNT(id) FROM users");

然后

$count = mysql_result($query, 0);

当您期望包含多个数据的单行结果时,使用

$result = mysql_fetch_row($query); => $result[0], $result[1], $result[2] etc

或者

$result = mysql_fetch_assoc($query); => $result['type'], $result['name'] etc

当您期望包含多个数据的多行结果时,二手

while ($result = mysql_fetch_row($query)) {
    => $result[0], $result[1], $result[2] etc
}

while ($result = mysql_fetch_assoc($query)) {
    => $result['type'], $result['name'] etc
}
  1. mysql_result() works when you do mysql_close() before ?

  2. Why not use mysql_fetch_row() here ?

EDIT WITH MORE INFOS for @XcodeDev :

You can use mysql_result() when you need a single result, for example :

$query = mysql_query("SELECT COUNT(id) FROM users");

Then

$count = mysql_result($query, 0);

When you expect a single line of result with several data, used

$result = mysql_fetch_row($query); => $result[0], $result[1], $result[2] etc

Or

$result = mysql_fetch_assoc($query); => $result['type'], $result['name'] etc

When you expect several lines of results with several data, used

while ($result = mysql_fetch_row($query)) {
    => $result[0], $result[1], $result[2] etc
}

Or

while ($result = mysql_fetch_assoc($query)) {
    => $result['type'], $result['name'] etc
}
我家小可爱 2024-10-28 11:19:34

对于初学者来说,您有一个无限循环,因为 $i 没有在循环内递增。如果你增加 $i,它应该可以解决问题。 $im是什么?

代码应如下所示:

<?php
$i=0;
while ($i < 843) {
    $type = mysql_result($result,$i,"type");
    $name = mysql_result($result,$i,"name");
    $street = mysql_result($result,$i,"street");
    $addr1 = mysql_result($result,$i,"address1");
    $addr2 = mysql_result($result,$i,"address2");
    $town = mysql_result($result,$i,"town");
    $county = mysql_result($result,$i,"county");
    $postcode = mysql_result($result,$i,"postcode");
    $number = mysql_result($result,$i,"number");
    $latlong = mysql_result($result,$i,"latlong");

?>

<tr>
<td><font face="Futura"><?php echo $type;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $street;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr1;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr2;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $town;?></font></td>
</tr>
<?php
    $i++;
} // this is where the loop is ending.
?>
<?
mysql_close(); // close mysql connection after reading data
echo "</table>"; 
?>

For a starter, you are having an infinite loop as $i is not being incremented inside the loop. If you increment $i, it should fix the problem. What is $im?

The code should look like:

<?php
$i=0;
while ($i < 843) {
    $type = mysql_result($result,$i,"type");
    $name = mysql_result($result,$i,"name");
    $street = mysql_result($result,$i,"street");
    $addr1 = mysql_result($result,$i,"address1");
    $addr2 = mysql_result($result,$i,"address2");
    $town = mysql_result($result,$i,"town");
    $county = mysql_result($result,$i,"county");
    $postcode = mysql_result($result,$i,"postcode");
    $number = mysql_result($result,$i,"number");
    $latlong = mysql_result($result,$i,"latlong");

?>

<tr>
<td><font face="Futura"><?php echo $type;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $street;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr1;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr2;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $town;?></font></td>
</tr>
<?php
    $i++;
} // this is where the loop is ending.
?>
<?
mysql_close(); // close mysql connection after reading data
echo "</table>"; 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文