douyeyan0650 2014-09-25 23:53
浏览 77

使用两个表显示多行

I'm not that good in programming PHP, and still learning from it.
Here's my problem, I need to display the result of rows from two different tables, had researched things and tried but all failed.

Hope someone could give some advice with my line of code.

$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild* FROM tblparent";
$num_results = $result->num_rows;
$result = $mysqli->query( $query );
if( $num_results ){
        echo "<center><table border='1' id='members'>";
                echo "<tr>";
                        echo "<th>Parent ID</th>";
                        echo "<th>Parent Firstname</th>";
                        echo "<th>Parent Lastname</th>";
                        echo "<th>Parent Middlename</th>";
                        echo "<th>Child ID</th>";
                        echo "<th>Child Firstname</th>";
                        echo "<th>Child Middlename</th>";
                        echo "<th>Child Lastname</th>";
                        echo "<th>Action</th>";
                echo "</tr>";
        while( $row = $result->fetch_assoc() ){
                extract($row);
                echo "<tr>";
                        echo "<td>{$Parent_ID}</td>";
                        echo "<td>{$PFname}</td>";
                        echo "<td>{$PLname}</td>";
                        echo "<td>{$PMname}</td>";
                        echo "<td>{$Child_ID}</td>";
                        echo "<td>{$CFname}</td>";
                        echo "<td>{$CMname}</td>";
                        echo "<td>{$CLname}</td>";
                        echo "<td>";
                                echo "<a href='#' onclick='delete_mem( {$Parent_ID} );'>Delete</a>";
                        echo "</td>";
                echo "</tr>";
        }
        echo "</table>";
}
else{
        echo "No records found.";
}
$result->free();
$mysqli->close();
  • 写回答

1条回答 默认 最新

  • doudao7113 2014-09-26 00:03
    关注

    I see two mistakes:

    1. you should satisfy the right order of statements, $result should be before $num_results assigment.
    2. it seems that there is a mistake in your SQL query.

    You need to adjust the following code, I am assuming that tblparent has an id and tblchild has a relation to tblparent id as parent_id:

    $query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild WHERE tblparent.id = tblchild.parent_id";
    $result = $mysqli->query( $query );
    $num_results = $result->num_rows;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?