Where和Order By子句不起作用

时间:2014-04-29 20:01:38

标签: php

所以这段代码不起作用,我无法弄明白。

$productid = (isset($_REQUEST['productId'])) ? $_REQUEST['productId'] : '';

$query = "SELECT * FROM products WHERE productId = '$productid' ORDER BY rand() limit 3";

1 个答案:

答案 0 :(得分:0)

也许试试:

$productid = (isset($_REQUEST['productId'])) ? $_REQUEST['productId'] : '';
if ($productid != ''){
    $query = "SELECT * FROM products WHERE productId = '$productid' ORDER BY rand() limit 3";
} else {
    $query = "SELECT * FROM products ORDER BY rand() limit 3";
}

如果productId没有结束,这应该返回所有产品。