Well, OK I think your problem is here: You sql *could* and probably does return more than one row. Yet, your code as it is now will only return the first row.
You need to cycle through the result rows with a while statement, like this: while ($myrow = mysql_fetch_assoc($result)) { $my_id = $myrow["shipping"]; echo $my_id; echo <br>; } Now, this will probably be a bit difficult to incorporate "as is" into your function, as you will have multiple "returns", and each will override the previous value, so you'll end up with only the last row's id.. Some pointers: Use something like $result = mysql_query($sql) instead of saying $shipping = mysql_query() It will work the same, but it is easier to read throuhgh your code and see where you actually invoke a result, and where you are simply assigning a value to a variable ( which you are NOT doing with this command, $result only contains the result set identifier of the query, not the actual result, that's why you pass the $result to a mysql_fetch_assoc(), only then do you have any kind of "workable" data) Next, use mysql_fetch_assoc(), instead of ...fetch_array() Difference is: array => [0,pointer1,value1,1,pointer2,value2,2,pointer3,value3.....] associative array => [pointer1,value1,pointer2,value2,pointer3,value3 ....] So, they hold the same data, but assoc's are "smaller" as they don't have the numerical pointer, which you don't use anyway. When you do the $my_id = $myrow["shipping"]; what you are doing is retrieving the "shipping-th" value from the array, and you would never do a $my_id = $myrow[2]; for instance... So, assoc's are generally better to use. You can safely change it in your code, the previous errors you got was not because of this, it was simply because your result set was empty, and mysql_fetch_array OR mysql_fetch_assoc would have given the error... I would suggest you try to accomplish your goal without a function at this point as to allow you to understand what you are trying to do better. You will have to either build a loop into your function, or call your function from within a loop, and both can be quite difficult to grasp initially... On Fri, 2002-11-01 at 12:24, Steve Jackson wrote: > Ok thanks. > Still having problems though. > This is the function now: > function get_shipping_cost() > { > //Get order ID from DB to pass to receipt. > $conn = db_connect(); > //$query = "select * from receipts order by receipt_id DESC > LIMIT 0,1"; > $query = "select shipping from receipts order by receipt_id DESC > LIMIT 0,1"; > $shipping = mysql_query($query); > $myrow = mysql_fetch_array($shipping); > $my_id = $myrow["shipping"]; > $shipping_cost = $my_id; > return $shipping_cost; > } > Still zero DB results. There is definitely a connection as DB_connect() > works. > > > No!!! > > Why are you doing this? > > $my_id = $myrow["shipping"]; > > > $my_id = $shipping_cost;<<<<<<<<< > > > return $shipping_cost; > > > > $my_id already contains the value you want, just return > > $my_id, the reason it doesn't work now, is because > > $shipping_cost has no value. If you really want to have the > > value stored in $shipping_cost, you must > > do: > > $shipping_cost = $my_id. > > > > You see, $my_id has a value in it the moment you say: > > $my_id = $my_row["shipping"]; > > but when you do $my_id = $shipping_cost, you are overwriting > > the value inside $my_id with $shipping_cost, which is like > > saying $my_id = 0; > > > > > > > > > > On Fri, 2002-11-01 at 11:07, Steve Jackson wrote: > > > Okay. Now I have this and it still returns nothing. > > > > > > function get_shipping_cost($shipping_cost) > > > { > > > //Get order ID from DB to pass to receipt. > > > $conn = db_connect(); > > > $query = "select shipping from receipts order by > > receipt_id DESC > > > LIMIT 0,1"; > > > $shipping = mysql_query($query); > > > $myrow = mysql_fetch_array($shipping); > > > $my_id = $myrow["shipping"]; > > > $my_id = $shipping_cost; > > > return $shipping_cost; > > > } > > > > > > And I still get one added to the variable in this function > > which pulls > > > orderid from the same table. > > > > > > function get_order_id_receipt($orderid) > > > { > > > //Get order ID from DB to pass to receipt. > > > $conn = db_connect(); > > > $query = "select orderid from receipts order by > > receipt_id DESC LIMIT > > > 0,1"; > > > $order = mysql_query($query); > > > $myrow = mysql_fetch_array($order); > > > $my_id = $myrow["orderid"]; > > > $my_id = $orderid; > > > return $orderid; > > > } > > > > > > Well and truly confused! > > > > > > Steve Jackson > > > Web Developer > > > Viola Systems Ltd. > > > http://www.violasystems.com > > > [EMAIL PROTECTED] > > > Mobile +358 50 343 5159 > > > > > > > > > > > > > -----Original Message----- > > > > From: Petre Agenbag [mailto:internet@;vsa.co.za] > > > > Sent: 1. marraskuuta 2002 10:23 > > > > To: [EMAIL PROTECTED] > > > > Cc: [EMAIL PROTECTED] > > > > Subject: Re: [PHP] Simple Problems taking me hours to solve! > > > > > > > > > > > > No, it should not return what you want. > > > > When you do > > > > $orderid = mysql_query($query); > > > > all this does is actually execute the query, and returns an > > > > array containing the actual result set. > > > > > > > > What you need to do from here is to do: > > > > > > > > $myrow = mysql_fetch_assoc($orderid); > > > > Now, what you have is an associative array in $myrow > > > > containing the row with identifiers being the collumn names > > > > in the db, > > > > > > > > So, from your query, you should do: > > > > > > > > $my_id = $myrow["orderid"]; > > > > > > > > $my_id will now contain the id you want... > > > > > > > > > > > > On Fri, 2002-11-01 at 10:21, Steve Jackson wrote: > > > > > This is the query I am running on my database: > > > > > > > > > > $query = "select orderid from receipts order by receipt_id > > > > DESC LIMIT > > > > > 0,1"; $orderid = mysql_query($query); > > > > > > > > > > That should return the last record in the DB? Correct? > > > > > > > > > > I currently get a number returned which is the number > > plus 1. IE > > > > > if > > > > > 423 is the last DB record then 424 is returned. > > > > > > > > > > Any ideas why this happens? > > > > > > > > > > Also in the same table I have a shipping price > > variable. My query > > > > > for this is: $query = "select shipping from receipts order by > > > > > receipt_id > > > > DESC LIMIT > > > > > 0,1"; $shipping_cost = mysql_query($query); > > > > > > > > > > Again this should return the last record? > > > > > It doesn't return anything however. Any ideas? > > > > > > > > > > Steve Jackson > > > > > Web Developer > > > > > Viola Systems Ltd. > > > > > http://www.violasystems.com <http://www.violasystems.com/> > > > > > [EMAIL PROTECTED] Mobile +358 50 343 5159 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > PHP General Mailing List (http://www.php.net/) > > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php