带有条件语句的mysqli fetch_assoc

时间:2014-05-15 04:48:51

标签: php mysql

我试图使用fetch_assoc和一些条件语句在我的页面上显示查询的多个部分。我的第一个while循环显示正确的信息,但循环2和3没有显示任何信息。过滤结果的正确方法是什么?

require_once('inc/connection.inc.php');

//create database connection
$conn = dbConnect();

//create SQL
$sql = 'SELECT * FROM menu';

$result = $conn->query($sql) or die(mysqli_error());
while($row = $result->fetch_assoc()){

if($row['category'] == 'appetizers'){ 
    echo $row['title']; 
    echo $row['price'];
}  
     

}

while($row = $result->fetch_assoc()){

if($row['category'] == 'salads'){ 
    echo $row['title']; 
    echo $row['price'];
}  
     

}

while($row = $result->fetch_assoc()){

if($row['category'] == 'desserts'){ 
    echo $row['title']; 
    echo $row['price'];
}  
     

}

4 个答案:

答案 0 :(得分:2)

为什么不这样做:

while($row = $result->fetch_assoc()){

   if($row['category'] == 'appetizers'){ 
     echo '<h1>Appetizers</h1>';
     echo $row['title']; 
     echo $row['price'];
   }  

   if($row['category'] == 'salads'){ 
     echo '<h1>Salads</h1>';
     echo $row['title']; 
     echo $row['price'];
   }  

   if($row['category'] == 'desserts'){ 
     echo '<h1>Desserts</h1>';
     echo $row['title']; 
     echo $row['price'];
   } 

}

即,使用单个while循环中的条件。

OR

您可以使用mysqli_data_seek重置指向第一条记录的指针并多次重复使用相同的资源

while($row = $result->fetch_assoc()){

   if($row['category'] == 'appetizers'){ 
     echo $row['title']; 
     echo $row['price'];
   }  
}

mysqli_data_seek($result,0);
while($row = $result->fetch_assoc()){

   if($row['category'] == 'salads'){ 
     echo $row['title']; 
     echo $row['price'];
   }
}

mysqli_data_seek($result,0);
while($row = $result->fetch_assoc()){

   if($row['category'] == 'desserts'){ 
     echo $row['title']; 
     echo $row['price'];
   }
}

答案 1 :(得分:0)

while($row = $result->fetch_assoc())
{

  if($row['category'] == 'appetizers'){ 
    echo $row['title']; 
    echo $row['price'];
  }  

  if($row['category'] == 'salads'){ 
    echo $row['title']; 
    echo $row['price'];
  }  
  if($row['category'] == 'desserts'){ 
    echo $row['title']; 
    echo $row['price'];
  }  
}

答案 2 :(得分:0)

您正在运行一次查询结果并将其取回三次,因此需要三次查询结果

$result = $conn->query($sql) or die(mysqli_error());
$result1 = $conn->query($sql) or die(mysqli_error());
$result2 = $conn->query($sql) or die(mysqli_error());

然后循环

while($row = $result->fetch_assoc()){
while($row1 = $result1->fetch_assoc()){
while($row2 = $result2->fetch_assoc()){

你也可以在循环中使用多个if(),如: -

while($row = $result->fetch_assoc()){
if($row['category'] == 'appetizers'){ 
    echo $row['title']; 
    echo $row['price'];
}
if($row['category'] == 'salads'){ 
    echo $row['title']; 
    echo $row['price'];
}
if($row['category'] == 'desserts'){ 
    echo $row['title']; 
    echo $row['price'];
}
}

答案 3 :(得分:0)

我认为你所追求的是这样的......

$categories = ['appetizers', 'salads', 'desserts'];

if (!$stmt = $conn->prepare('SELECT title, price FROM menu WHERE category = ?')) {
    throw new Exception($con->error, $con->errno);
}

$stmt->bind_param('s', $category);

foreach ($categories as $category) {
    if (!$stmt->execute()) {
        throw new Exception($stmt->error, $stmt->errno);
    }
    $stmt->bind_result($title, $price);

    while($stmt->fetch()) {
        echo $title, $price;
    }
}

我所做的是为$categories数组中的每个类别执行预准备语句。这会按照您想要的顺序将每个类别下的menu行组合在一起。