【发布时间】:2020-03-09 02:32:15
【问题描述】:
我有两张桌子 第一个具有主键 c_id 的表。 第二个表有自己的主键和外键c_id(表2可以有多个与表1相关的条目)
我正在尝试查询第一个表中的所有记录,但也查找第二个表中与主键匹配的所有记录,并将其列中的一列回显到查询中的列。
这是我的代码 注意:对连接表不熟悉,因此以下代码只是回显表 1 中的记录。
<?php
include_once('connected.php');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM files ORDER BY c_id Desc";
if ($result=mysqli_query($conn,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Total = %d \n",$rowcount);
// Free result set
mysqli_free_result($result);
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td> <b>". $row["c_no"]." </b>
<td> <b>". $row["c_applicants"]. " </b> //this rows should echo all records from 2nd table
<td> <b>". $row["c_details"]. " </b>
<td> <b>". $row["c_for"]. " </b>
<td> <b>". $row["c_person"]. " </b>
<td> <b>". $row["c_country"]. "</b>
<td> <b>". $row["c_rdate"]. " </b>
<td> <b>". $row["c_galdate"]. "</b>
<td> "."<a href=viewfile.php?id=". $row['c_id'] .">Expand</a>". "<br>";
}
} else {
echo " results";
}
$conn->close();
?>
【问题讨论】: