1

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> 
2
  • What is table 1? Shouldn't you have an "detectinglocations AS 1"? Commented Jul 5, 2012 at 17:21
  • @SableFoste: It's actually the letter "L". As for the... AS... it's implicit :) Commented Jul 5, 2012 at 17:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @minitech, thank you for taking the time to reply to my post, it works great. Thank you also for the guidance re. the MySQL extensions. Kind regards

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.