Jump to content

Warning: mysql_fetch_assoc() expects parameter 1 to be resource


Eiolon

Recommended Posts

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.