Florence Mae Ragasa
Florence Mae Ragasa

Reputation: 1

Displaying Multiple Rows with two tables

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();

Upvotes: 0

Views: 413

Answers (1)

CSchulz
CSchulz

Reputation: 11030

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;

Upvotes: 1

Related Questions