Eiolon Posted July 24, 2010 Share Posted July 24, 2010 I am getting this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, array given in C:\wamp\www\purchase.php on line 52 Line 52 in question: <?php } while ($items = mysql_fetch_assoc($items)); ?> My query: $query_items = "SELECT id, qty, description, price, total FROM items WHERE purchase_id = 8"; $items = mysql_query($query_items) OR die ('Cannot retrieve a list of line items.'); $row_items = mysql_fetch_assoc($items); <table class="data_grid"> <tr> <th width="50">QTY</th> <th>DESCRIPTION</th> <th width="125">PRICE</th> <th width="125">TOTAL</th> <th width="50"></th> </tr> <?php do { ?> <tr> <td width="50"><div align="center"><?php echo $row_items['qty']; ?></div></td> <td><?php echo $row_items['description']; ?></td> <td width="125"><div align="center">$<?php echo $row_items['price']; ?></div></td> <td width="125"><div align="center">$<?php echo $row_items['total']; ?></div></td> <td width="50"><div align="center"><img src="images/delete.png" alt="Delete Line"/> <img src="images/edit.png" alt="Edit Line"/></div></td> </tr> <?php } while ($items = mysql_fetch_assoc($items)); ?> I have 4 entries for the purchase_id. It is displaying the first row twice, and the other 3 arent shown. Link to comment https://forums.phpfreaks.com/topic/208772-warning-mysql_fetch_assoc-expects-parameter-1-to-be-resource/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2010 Share Posted July 24, 2010 <?php } while ($items = mysql_fetch_assoc($items)); ?> You are overwritting the result resource the first time your while() condition is evalueate. The second time, $items no longer contains a result resource. Since you are using $row_items in your actual code, I suspect you meant to use $row_items = mysql_fetch_assoc($items). You will find that using a do/while loop requires more lines of code to accomplish the same thing as using a regular while(){} loop and in this case would have likely prevented this error in your code because you would not have needed to type the $row_items = mysql_fetch_assoc($items) twice. Link to comment https://forums.phpfreaks.com/topic/208772-warning-mysql_fetch_assoc-expects-parameter-1-to-be-resource/#findComment-1090656 Share on other sites More sharing options...
Eiolon Posted July 24, 2010 Author Share Posted July 24, 2010 Ha ha! Oh my I didn't even notice that. Thanks very much! Link to comment https://forums.phpfreaks.com/topic/208772-warning-mysql_fetch_assoc-expects-parameter-1-to-be-resource/#findComment-1090659 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.