1

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

警告:mysql_fetch_assoc():提供的参数不是第 94 行 /homepages/2/d333603417/htdocs/locationsconsole.php 中的有效 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> 
4

1 回答 1

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。

于 2012-07-05T17:20:07.357 回答