prepare('SELECT customer.id AS cid, name, phone, source ' . 'FROM customer JOIN customer_potential ON customer.id = customer_potential.id ' . 'ORDER BY cid'); $rs = $stmt->execute(); while($row = $rs->fetchArray()) { $name = $row['name']; $phone = $row['phone']; $source = $row['source']; echo "**future customer** $name, $phone, $source\r\n"; } $stmt = $con->prepare('SELECT customer.id AS cid,name,address1,address2,phone,order_main.id AS oid,status,SUM(quantity*price) AS sumval ' . 'FROM customer JOIN customer_actual ON customer.id = customer_actual.id ' . ' JOIN order_main ON customer.id = order_main.customer ' . ' JOIN order_line ON order_main.id = order_line.order_main ' . 'GROUP BY customer.id,name,address1,address2,phone,order_main.id,status ' . 'ORDER BY cid, oid'); $rs = $stmt->execute(); $last_cid = 0; while($row = $rs->fetchArray()) { $cid = $row['cid']; $name = $row['name']; $address1 = $row['address1']; $address2 = $row['address2']; $phone = $row['phone']; $oid = $row['oid']; $status = $row['status']; $sumval = $row['sumval']; if($cid != $last_cid) { echo "$name, $address1, $address2, $phone\r\n"; $last_cid = $cid; } echo " $oid, $status, $sumval\r\n"; } $con->close(); ?>