...

View Full Version : mysql_fetch_array(): supplied argument is not a valid MySQL result resource



irenaught
08-03-2008, 03:12 AM
Please help me... Here is the first part of the offending code.


if (isset($_REQUEST['complete'])) {
$order_complete_qry = "UPDATE orders SET order_status = '2' WHERE order_id =$oid[order_id]";
$result = mysql_query($order_complete_qry)or die ("Cannot complete order: ".mysql_error());

this works fine and updates the DB appropriately the following code immediately follows in the script as well



//series of queries to update stock
//this query should get all stock items added to order by user
$current_order_qry = "SELECT item_id FROM orderitems WHERE order_id= $oid[order_id] ORDER BY item_id";
$order_result = "mysql_query($current_order_qry) or die (\"Cannot get items on order: \".mysql_error())";

while($orderitems = mysql_fetch_array($order_result)){
$itemids[]=$orderitems;
}

this on gets a "mysql_fetch_array(): supplied argument is not a valid MySQL result resource in..." error. The update statement is designed to only pull one record and the select should draw multiples. I think therein lies the problem but im obviously not sure. Please help a pathetic newbie. If there is not enough code here let me know and I can post more I just didn't want to write a book for people to have to read.

derzok
08-03-2008, 03:20 AM
You're putting the whole command in quotes so it's a string - that's really not what you want.

This:


//series of queries to update stock
//this query should get all stock items added to order by user
$current_order_qry = "SELECT item_id FROM orderitems WHERE order_id= $oid[order_id] ORDER BY item_id";
$order_result = "mysql_query($current_order_qry) or die (\"Cannot get items on order: \".mysql_error())";

while($orderitems = mysql_fetch_array($order_result)){
$itemids[]=$orderitems;
}


Should be this:


//series of queries to update stock
//this query should get all stock items added to order by user
$current_order_qry = "SELECT item_id FROM orderitems WHERE order_id= $oid[order_id] ORDER BY item_id";
$order_result = mysql_query($current_order_qry) or die ("Cannot get items on order: ".mysql_error());

while($orderitems = mysql_fetch_array($order_result)){
$itemids[]=$orderitems;
}


You should consider using a syntax highlighting editor - these simple errors tend to stick out better.

oesxyl
08-03-2008, 03:21 AM
please, put your code between [ php] and [ /php] or [ code] and [ /code] tags. You can edit your posts, thank you.

try this:


//series of queries to update stock
//this query should get all stock items added to order by user
$current_order_qry = "SELECT item_id FROM orderitems WHERE order_id= $oid[order_id] ORDER BY item_id";
$order_result = mysql_query($current_order_qry) or die ("Cannot get items on order: ".mysql_error());
if($order_result){
while($orderitems = mysql_fetch_array($order_result)){
$itemids[]=$orderitems;
}
}else{
print mysql_error();
}


regards

irenaught
08-03-2008, 03:26 AM
I just noticed the thing about the php tags but thanks for letting me know and thanks for the help as well.

irenaught
08-03-2008, 03:45 AM
Ok we are much closer now thank you but i still have one problem...


echo "The items on the order are".".$itemids."."";

printf("The items on the order are : %s ", $itemids[item_id]);


the first line outputs array and the second one nothing at all. Is it possible to echo the items like this or does it have to be a print_r statement instead?

oesxyl
08-03-2008, 03:52 AM
echo "The items on the order are".$itemids['item_id'];


regards

irenaught
08-03-2008, 04:32 AM
Thank you for your help. I always want to use the "thank user for this helpful post" but then it says to make sure that a lot of thought and effort went into the post I dont feel like it took a lot of effort for you to answer my easy question but I still really appreciate it so what is the standard etiquette? Thanks one way or the other.

irenaught
08-03-2008, 04:49 AM
I copied and pasted your code and i still get "The items on the order are" without the quotes and then nothing. If i print-r it i do get what im looking for but i need to display it to a user and then interact with sql based on the item id's it contains. I know its getting the right data so where do i go next?

abduraooft
08-03-2008, 03:45 PM
while($orderitems = mysql_fetch_array($order_result)){
$itemids[]=$orderitems;

echo "The items on the order are".$itemids['item_id'];
Just have a look in to the output of print_r($itemids) statement
It'd be starting with something like

Array ( [0] => Array ( [0]......) [1] =>
From your code, it's clear that the array $itemids is going to be an array of array(2D), since $orderitems itself is an array(return value of mysql_fetch_array)
Thus

echo "The items on the order are";
for($i=0;$i<count($itemids);$i++)
echo "<br/>".$itemids[$i]['item_id'] ;
should give you some result.

irenaught
08-03-2008, 08:31 PM
I added the code but still no effect. You are definately right about it being an array of arrays and I feel this is the problem but the code you offered had no output still. The print_r statement outputs as you expect it would. Do i need to use some kind of explode() or implode() type line or maybe a foreach instead of for statement? Ive been trying different code bits for going on two days. Please if you have any other suggestions please throw them out there and thanks for the help so far.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum