Hi,

I need to print all the orders with the month in the Monthly Order Report.How should I do it.Can someone tel me how to do it...

This is my query.Order date should equal to the relavent month...

$this->result = mysql_query("SELECT * FROM tbl_order,tbl_order_items WHERE tbl_order.order_id=tbl_order_items.order_id AND tbl_order.order_status='accepted' AND order_date= ............... ORDER BY tbl_order.order_id");

Thanks in advance..:)

Dani AI

Generated

's quick filter for the current month solved the immediate need and confirmed it works. A few practical additions help the report run reliably and scale better: avoid applying functions to the date column in the WHERE clause (that blocks index use), use explicit JOINs instead of comma joins, and stop using the old mysql_* API in production code.

Compute the month boundaries in application code and pass them as parameters so the database can use an index on order_date. Example PHP to get the start and the exclusive end of a month:

$year  = 2010;
$month = 8;
$start = sprintf('%04d-%02d-01 00:00:00', $year, $month);
$end   = date('Y-m-d H:i:s', strtotime($start . ' +1 month'));  // exclusive end

Use a parameterized query (PDO or mysqli) and range conditions (>= $start and < $end) to avoid function-wrapped columns and to protect against SQL injection. Also select only needed columns instead of SELECT * so order rows do not get duplicated by item rows and the report payload stays small:

SELECT o.order_id, o.order_date, o.order_status, i.product_id, i.quantity
FROM tbl_order AS o
JOIN tbl_order_items AS i ON i.order_id = o.order_id
WHERE o.order_status = ? AND o.order_date >= ? AND o.order_date < ?
ORDER BY o.order_id;

Extra tips: add an index on order_date or a composite index like (order_status, order_date) if filtering by status is frequent; account for stored timezone (UTC vs local) when computing month bounds; if the report needs one row per order, fetch orders first and then items (or aggregate items with GROUPCONCAT) to avoid repeated order rows. For migration away from the deprecated `mysql*` API, consult the PHP manual on PDO/mysqli for prepared statements and safer database access (Choosing a MySQL API in PHP).

Recommended Answers

All 3 Replies

The simplest solution looks like the following

$this->result = mysql_query("SELECT * FROM tbl_order,tbl_order_items WHERE tbl_order.order_id=tbl_order_items.order_id AND tbl_order.order_status='accepted' AND MONTH(order_date)=MONTH(NOW()) and YEAR(order_date)=YEAR(NOW())
ORDER BY tbl_order.order_id");

I am assuming order_date is a DATE/DATETIME data type.

heyy thanks alot..:)
its working..

You are welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.