简体   繁体   English

如何从mysql数据库的两个表中查询数据并显示一个HTML表

[英]How to query data from two tables from mysql database and display one HTML table

I have two tables... Only $sql is executing. 我有两个表...仅执行$ sql。 How do I get $sql2 to query also? 我如何也获取$ sql2进行查询? $sql2 is storing the secondID with the current_timestamp in the database but not receiving any input from 'serialnumber.' $ sql2正在将具有current_timestamp的secondID存储在数据库中,但未从“ serialnumber”接收任何输入。 Only the first $sql is querying and storing in the database. 只有第一个$ sql正在查询并存储在数据库中。

       +--------------+
       | SafetyAtt    |       
       +--------------+             
       | SecondID    |
       | SerialNumber |
       +--------------+                    

          +---------+
          | Safety  |
          +---------+
          | ID      |
          | DateLog |
          | Topic   |
          +---------+

      <table>
        <thead>
        <tr align="left">
          <th>Seq</th>
          <th>Date</th>
          <th>Topic</th>
          <th># of Attendees</th>
          <th>Add Attendee</th>
        </tr>
        </thead>

    <?php
  $sql = "SELECT ID, DateLog, Topic FROM Safety";
  $sql2 = "SELECT secondID, SerialNumber FROM SafetyAtt";
  $result = $conn->query($sql);
  if (!$result) {
    trigger_error('Invalid query: ' . $conn->error);
  }

  if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["ID"]. "</td><td>" . $row["DateLog"] . "</td><td>"
    . $row["Topic"] . "</td><td>" . $row["secondID"] . "</td><td>"  ?>
      Serial Number: <input type="text" name="serialnumber" placeholder="Enter serial #"> <?php " . </td></tr>";
    }
    echo "</table>";
  } else {
   echo "0 results"; 
   }
  $conn->close();
    ?>

</table>

You have to JOIN your two tables and it should have one query. 您必须联接两个表,并且它应该具有一个查询。 First you select the columns you want to get, then join the tables by using the foreign key or something else that is common in the two tables. 首先,选择要获取的列,然后使用外键或两个表中常见的其他方法将表连接起来。 For example, 例如,

"SELECT car.id, driver.name, driver.age FROM car INNER JOIN driver ON car.driverID=driver.driverID"

see this link to more about SQL JOINS. 请参阅此链接以获取更多有关SQL JOINS的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM