I’m running a query on our customer order database to see the sales from the last week. The query looks like this:
select
distinct day(from_unixtime(customerorder.datetime)) as day,
count(distinct customerorder.customerorderid) as count_totalorders,
count(distinct customerorderpos.itemid) as count_differentitems,
sum(customerorderpos.quantity_ordered) as quantity_ordered,
sum(customerorderpos.itemsubtotal) as item_subtotal,
sum(customerorderpos.pricechangetotal) as item_pricechangetotal,
sum(customerorderpos.itemtotal) as item_total,
sum(customerorderpos.purchase_price * customerorderpos.quantity_ordered) as item_purchasepricetotal,
sum(distinct cart_discounttotal) as total_discount,
sum(customerorderpos.itemtotal - (customerorderpos.purchase_price * customerorderpos.quantity_ordered) - (select distinct cart_discounttotal)) as item_earningstotal,
sum(distinct cart_total_shipping) as total_shipping,
sum(distinct cart_total_tax) as total_tax,
sum(distinct cart_total_complete) as total_complete
from customerorder
inner join customerorderpos on customerorder.customerorderid = customerorderpos.customerorderid
where
customerorder.datetime >= 1351494000 and
customerorder.status_cancelled = "0"
group by day(from_unixtime(customerorder.datetime))
order by customerorder.datetime
When I enter the query in PhpMyAdmin in works fine and shows the last 7 days orders. But when we run it in our own software it gives an error.
$result = $db->query($query);
while ($row = $result->fetch_assoc()){
This shows: “Fatal error: Call to a member function fetch_assoc() on a non-object”
But…. if I take out “where customerorder.datetime >= 1351494000” from the query it works fine.
In other words the query works perfectly until I want to limit it to a specific time. Customerorder.datetime is an int(11) column with a timestamp.
Any ideas what the problem could be?
Thanks for your help!
Thanks for your help. I found the problem though, it was (of course) my own mistake!
The actual SQL syntax was fine, I just had an error in our own software (the $query was running though a clean-up function that replaced the “>=” characters, making the query invalid).
Sorry for wasting your time 🙁