如何在PHP中使用GROUP BY + ORDER BY + WHERE和GET一起使用?

问题描述 投票:-1回答:2

我尝试使用php制作销售报告文档并从mysql数据库连接它。 如何在PHP get方法中使用where + group by + order(mysql query)? 我可以使用SELECT和WHERE,但是如何添加GROUP BY和ORDER BY查询?

$date=$_GET['date'];
$query=mysql_query("select * from product_sold where date=" . $date);
while($lihat=mysql_fetch_array($query)){
    $pdf->Cell(1, 0.8, $no , 1, 0, 'C');
    $pdf->Cell(3, 0.8, $lihat['date'],1, 0, 'C');
    $pdf->Cell(6, 0.8, $lihat['product'],1, 0, 'C');
    $pdf->Cell(3, 0.8, $lihat['quantity'], 1, 0,'C');
    $pdf->Cell(4, 0.8, "Rp. ".number_format($lihat['price'])." ,-", 1, 0,'C');
php mysql post get where
2个回答
0
投票

你在sql语句的末尾使用GROUP BY和ORDER BY,就像这样......

"SELECT * FROM table WHERE date = $date ORDER BY desiredCategory "

此外,您真的需要使用预准备语句。您正在以这种方式开放sql注入攻击。

More about GROUP BY More about ORDER BY More about prepared statements


0
投票

您可以在WHERE过滤后添加GROUP BY,然后可以添加ORDER BY。

SELECT *
FROM product_sold
WHERE date = '$date'
GROUP BY id
ORDER BY product ASC

并注意像@jtylerm这样的sql注入说。

© www.soinside.com 2019 - 2024. All rights reserved.