IRHM
IRHM

Reputation: 1326

MySQL Into HTML Table With PHP

Using the example I found here I'm using the code below to create a table with PHP. However when I run this, I receive the following error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /homepages/2/d333603417/htdocs/locationsconsole.php on line 94

Line 94 of my code is while (($row = mysql_fetch_assoc($query)) !== false) {

Could someone perhaps tell me please, have I interpreted the code wrongly, or is there an error with code. I just wondered whether someone could perhaps take a look at this please and provide a little guidance on how I can rectify this issue.

<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> 

Upvotes: 1

Views: 145

Answers (1)

Ry-
Ry-

Reputation: 225291

You need to actually perform the query before fetching the results, using 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);

However, please note that the mysql_ extensions are discouraged and will be removed in future versions of PHP, so I suggest you switch to PDO or MySQLi if at all possible.

Upvotes: 1

Related Questions