【问题标题】:mysql php if else inside while loop [closed]mysql php if else 在while循环中[关闭]
【发布时间】:2014-03-30 16:14:36
【问题描述】:
$sql = mysql_query("SELECT DISTINCT business_region FROM business WHERE business_category='occupation'");
// count the output amount
$businessCount = mysql_num_rows($sql);
if ($businessCount > 0) {
  while ($row = mysql_fetch_array($sql)) { 
      $business_region = $row["business_region"];          
      $Region_view .= "display value";
  }
}

这给出了以下结果:

密歇根多伦多俄亥俄曼彻斯特伦敦悉尼巴黎

我只想选择Ohio and London 作为变量值:

$Region_view .

也许我可以使用 if else 结构,但我无法让它工作。

即我需要只选择Ohio, London

我可以使用联合 MySQL 语句来做到这一点,但它太慢了,所以我需要并要求另一种方式。

【问题讨论】:

  • 为什么只有“俄亥俄,伦敦”?你能澄清一下你的问题吗?你想完成什么?
  • if ($businessCount > 0) - 这绝对没用
  • 操作慢很可能是DISTINCT没有在查询的WHERE子句中添加AND business_region IN ( 'Ohio', 'London' )

标签: php mysql if-statement while-loop


【解决方案1】:
$sql = mysql_query("SELECT DISTINCT business_region FROM business WHERE business_category ='occupation'");
// count the output amount
$businessCount = mysql_num_rows($sql); 
if ($businessCount > 0) {
  while($row = mysql_fetch_array($sql)){ 
    $business_region = $row["business_region"];         
    if ($business_region == 'Ohio' || $business_region == 'London') // added
    $Region_view .= $business_region;//changed
  }
}

【讨论】:

  • @AndréLaszlo 他自己的,将在 2015 年成为行业标准
  • @Najzero 愿上帝满足你所有的愿望。
  • 太棒了!!!它工作得很好,做得很好,伙计
  • @user3106822 您应该将其标记为已接受,以便其他人知道它可以完美运行。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 2015-11-08
  • 1970-01-01
  • 2015-11-13
  • 2020-08-14
相关资源
最近更新 更多