【问题标题】:Echo two SQL query results on a table在一个表上回显两个 SQL 查询结果
【发布时间】:2019-03-14 01:46:26
【问题描述】:

我有以下代码:

<?php
  include_once '../includes/db.inc.php';
  $sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
  $result = mysqli_query($conn, $sql);
  $resultCheck = mysqli_num_rows($result);

  if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
      $first = $row["prm_nome"];
      $last = $row["apelido"];
      $phone = $row['nmr_tlm'];
      $email = $row['mail'];
      $nif = $row['nif_id'];
      $flight = "SELECT flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id";

      echo '<tr>';
      echo '<td><a href="detail.php?id='. $nif . '">'.$nif.'</a></td>';
      echo '<td>'.$first.'</td>';
      echo '<td>'.$last.'</td>';
      echo '<td>'.$phone.'</td>';
      echo '<td>'.$email.'</td>';
      echo '<td><a href="../flights/detail.php?id='. $flight . '">'.$flight.'</a></td>';
      echo '</tr>';
    }
  }
 ?>

我需要回显SELECT flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id 查询的结果。但是当我保存文件时,我在页面上看到的是与该查询而不是结果的链接。

我是否应该在第一个 $sql = 下使用该查询开始一个新的 $sql =? 还是有其他方法?

我尝试了UNIONSELECT *, flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id FROM clients ORDER BY nif_id ASC;,但后来我得到了mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given

【问题讨论】:

  • 您永远不会执行$flight 查询。
  • 内部查询是否应该依赖于外部查询中的行?

标签: mysql echo


【解决方案1】:

您不需要两个查询。只需使用连接查询。

<?php
include_once '../includes/db.inc.php';
$sql = "SELECT c.prm_nome, c.apelido, c.nmr_tlm, c.mail, c.nif_id, f.flight_id
        FROM clients c
        JOIN flights f ON f.nif_id = c.nif_id
        ORDER BY c.nif_id ASC;";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $first = $row["prm_nome"];
        $last = $row["apelido"];
        $phone = $row['nmr_tlm'];
        $email = $row['mail'];
        $nif = $row['nif_id'];
        $flight = $row['flight_id'];

        echo '<tr>';
        echo '<td><a href="detail.php?id='. $nif . '">'.$nif.'</a></td>';
        echo '<td>'.$first.'</td>';
        echo '<td>'.$last.'</td>';
        echo '<td>'.$phone.'</td>';
        echo '<td>'.$email.'</td>';
        echo '<td><a href="../flights/detail.php?id='. $flight . '">'.$flight.'</a></td>';
        echo '</tr>';
    }
}
?>

【讨论】:

  • 我仍然收到mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given
  • @Always Sunny 解决方案成功了。现在我将尝试了解我需要在echo '&lt;td&gt;&lt;a href="../flights/detail.php?id='. $flight . '"&gt;'.$flight.'&lt;/a&gt;&lt;/td&gt;'; 上更改什么。谢谢!
  • 我打错了,ORDER BY cnif_id 应该是 ORDER BY c.nif_id。现在试试这个。
  • 他的解决方案将打印每个客户的所有客户航班,而不仅仅是该客户的航班。
  • 刚刚试过你的代码,它仍然给我mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
相关资源
最近更新 更多