【问题标题】:MySQL Into HTML Table With PHP使用 PHP 将 MySQL 转换为 HTML 表
【发布时间】:2018-07-11 17:46:46
【问题描述】:

使用我找到的示例here 我正在使用下面的代码使用 PHP 创建一个表。但是,当我运行它时,我收到以下错误:

警告:mysql_fetch_assoc():提供的参数不是 /homepages/2/d333603417/htdocs/locationsconsole.php 第 94 行中的有效 MySQL 结果资源

我的代码的第 94 行是while (($row = mysql_fetch_assoc($query)) !== false) {

有人可以告诉我,我是否错误地解释了代码,或者代码有错误。我只是想知道是否有人可以看看这个,并就我如何纠正这个问题提供一些指导。

<form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
<?php
$query = "SELECT  l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '27'  GROUP BY l.locationname";
?>

<table>
<thead>
<tr>
    <th width="62"><div align="center">Location Name</div></th>
    <th width="120"><div align="left">Location Address</div></th>
    <th width="81"><div align="center">No. Of Finds Made </div></th>                            
</tr>
</thead>
<tbody>
<?php
    while (($row = mysql_fetch_assoc($query)) !== false) {
?>
<tr>
<td><?php echo $row['locationname']; ?></td>
  <td><?php echo $row['returnedaddress']; ?></td>
    <td><?php echo $row['totalfinds']; ?></td>
    </tr>
<?php
}
?>
</tbody>
</table>
</form> 

【问题讨论】:

  • 什么是表1?你不应该有一个“detectinglocations AS 1”吗?
  • @SableFoste:实际上是字母“L”。至于...AS...这是隐含的:)

标签: php mysql html-table


【解决方案1】:

您需要在获取结果之前实际执行查询,使用mysql_query

$sql = "SELECT  l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '27'  GROUP BY l.locationname";
$query = mysql_query($sql);

但是,请注意 mysql_ 扩展不被鼓励,并且会在 PHP 的未来版本中被删除,所以我建议你尽可能切换到 PDO 或 MySQLi。

【讨论】:

  • 嗨@minitech,感谢您抽出宝贵时间回复我的帖子,效果很好。也谢谢你的指导。 MySQL 扩展。亲切的问候
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多