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.

Link to comment
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.