【问题标题】:Nested 'while' loop needs to return information based on two query results being equal嵌套的“while”循环需要根据两个查询结果相等返回信息
【发布时间】:2014-11-30 17:02:35
【问题描述】:

我正在尝试显示在反映办公室所在州的标题下排序的一组办公室,如下所示:

State
 Office
 Office
 ...
State
 Office
 ...

我反对这个已经有一段时间了。 MySQL 数据库中有一个表“offices”,其中每个办公室的状态都列在“office_state”下。这是我想出的 - 但没有返回任何内容。

// Here I return a list of the distinct States:
$stateQuery = 'SELECT DISTINCT office_state AS state FROM offices';
$stateResult = mysql_query($stateQuery);

// Here I return each office's information:
$officeQuery = 'SELECT office_information, office_state FROM offices';
$officeResult = mysql_query($officeQuery);

// And here's where I'm stuck:
while ($rows = mysql_fetch_assoc($stateResult)) {

    // This returns the distinct States:
    echo $rows['state'];

    // Here is my effort at listing each office within the State:
    $rows1 = mysql_fetch_assoc($officeResult);
    if ($rows['state'] == $rows1['office_state']) {
      echo $rows1['office_information'];

    }      
}

任何帮助将不胜感激。

【问题讨论】:

  • 什么都不返回在哪里?你在检查错误吗?

标签: php mysql


【解决方案1】:

试试这个

<?php
// Here I return a list of the distinct States:
$stateQuery = 'SELECT DISTINCT office_state AS state FROM offices';
$stateResult = mysql_query($stateQuery);

// Here I return each office's information:
$officeQuery = 'SELECT office_information, office_state FROM offices';
$officeResult = mysql_query($officeQuery);

// And here's where I'm stuck:
while ($rows = mysql_fetch_array($stateResult)) {

    // Here is my effort at listing each office within the State:
    while($rows1 = mysql_fetch_array($officeResult)){
        if ($rows['state'] == $rows1['office_state']) {
          echo $rows1['office_information'];
        }
    }      
}
?>

【讨论】:

猜你喜欢
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2019-09-28
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多