在PHP中,如何仅选择数量大于0的项目? [关闭]

$quantity=$_POST['quantity'];

do{

 $request="SELECT id, name, type, price FROM `corro`.`food`";

$result=mysql_query($request);

}while(quantity>0)

Based on your comment I assume it's this:

$quantity=$_POST['quantity'];
$request="SELECT id, name, type, price FROM `corro`.`food` WHERE quantity>0";
$result=mysql_query($request);
$quantity=$_POST['quantity'];
if($quantity>0){
  $request="SELECT id, name, type, price FROM `corro`.`food`";
  $result=mysql_query($request);
}

First, you have a syntax error :

while (quantity > 0) 

should be :

while ($quantity > 0)

But If you execute your code, you'll have an infinite loop if $quantity is actually greater than 0 : this will only execute your query over and over again because you never decrement $quantity.

I'm guessing that you have a quantity column in your table so what you should do is put the condition in your SQL query. Using PDO that would give you :

$stmt = $dbh->prepare("SELECT id, name, type, price FROM food WHERE quantity > ?");
$stmt->execute(array($_POST['quantity']));
$results = $stmt->fetchAll();

EDIT (following the latest answers)

Note that mysql_* are deprecated as of PHP 5.5.0. You should use mysqli or PDO.

Just do it in your mysql_query

Please note that mysql_* functions are now deprecated, and should not be used. Use mysqli instead. The query for mysql would be SELECT id, name, type, price FROM corro.food WHERE quantity>0

Your while loop is running unlimited times try giving condition to your code as in below demo code

  $quantity=$_POST['quantity'];
  $quantity_count = 0;
  do{
        $request="SELECT id, name, type, price FROM `corro`.`food`";
        $result=mysql_query($request);
        $quantity_count++;
  }while($quantity>0 && $quantity_count < 10);     //This is demo code