Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将另一个mysqli查询添加到while循环中?_Php_While Loop - Fatal编程技术网

Php 如何将另一个mysqli查询添加到while循环中?

Php 如何将另一个mysqli查询添加到while循环中?,php,while-loop,Php,While Loop,因此,我执行一个查询并以表的形式返回结果。下面的代码运行良好 <table> <thead> <tr style="text-align: center;"> <th>Activity</th> <th>Description</th> <th>Frequency</th>

因此,我执行一个查询并以表的形式返回结果。下面的代码运行良好

        <table>
      <thead>
        <tr style="text-align: center;">
          <th>Activity</th>
          <th>Description</th>
          <th>Frequency</th>
          <th>Mandatory</th>
          <th>Added Yet</th>
        </tr>
      </thead>
      <tbody>
        <?php
          $stmt = $conn->prepare("SELECT * FROM knowledgebase ORDER BY category ASC");
          $stmt->execute();
          $result = $stmt->get_result();
          if($result->num_rows === 0) echo "<tr><td>No activities found</td><td></td><td></td><td></td><td></td><td></td></tr>           </tbody>
                  </table></br></br>
          Why not add your first activity from our <a href=\"#\">Knowledge base</a> or <a href=\"create_activities.php\">create a new activity</a> of your own";
          else {
          while($row = mysqli_fetch_array($result)) {
              $activity_id          =   $row['id'];
              $title                =   $row['title'];
              $description          =   $row['description'];
              $frequency            =   $row['frequency'];
              $mandatory            =   $row['mandatory'];

              echo "<tr><td><a href=\"add_activities.php?id=".$activity_id."\">".$title."</a></td><td>".$description."</td><td style=\"text-align: center;\">".$frequency."</td><td style=\"text-align: center;\">".$mandatory."</td><td></td></tr>";
            }
          }
           $stmt->close();
         ?>
      </tbody>
    </table>

活动
描述
频率
强制性的
增加了吗
我想添加的是final
中的另一个查询。我要做的是查询第二个db表,并说如果此活动已添加到users表中,则返回YES,否则返回NO

问题是,将查询添加到while循环会不断抛出错误


感谢您的建议。

您可以在第一次查询中将users表加入knowledgebase表,而不是对每一行运行另一个查询。如果使用左联接,仍然可以从knowledgebase获取所有行

SELECT knowledgebase.*, userstable.activity_id
FROM knowledgebase
     LEFT JOIN userstable ON knowledgebase.id = userstable.activity_id
ORDER BY category ASC
然后,在上一个
中,您可以根据用户表中是否有匹配行打印是/否

...<td><?php echo $row['activity_id'] ? 'YES' : 'NO' ?></td>...
。。。。。。

(我为您的其他表和列指定了名称,但我认为这显示了大致思路。)

您可以在第一次查询中将users表与knowledgebase表连接起来,而不是对每一行运行另一个查询。如果使用左联接,仍然可以从knowledgebase获取所有行

SELECT knowledgebase.*, userstable.activity_id
FROM knowledgebase
     LEFT JOIN userstable ON knowledgebase.id = userstable.activity_id
ORDER BY category ASC
然后,在上一个
中,您可以根据用户表中是否有匹配行打印是/否

...<td><?php echo $row['activity_id'] ? 'YES' : 'NO' ?></td>...
。。。。。。

(我为您的其他表和列编写了名称,但我认为它显示了总体思路。)

添加您的代码和您遇到的错误,然后我们可以帮助添加您的代码和您遇到的错误,然后我们可以帮助这很好。非常感谢。太好了。非常感谢。