0

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
  • Just to confirm, you're trying to get data from 2 DIFFERENT database tables, and put them into ONE HTML table? Commented Sep 26, 2014 at 0:03

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

btw, can I ask another question? Is it possible that if I delete the Parent_ID from tblParent, the record from tblChild from which it has a relation would be deleted too? Or i need to delete them both..
If it is the right answer, please upvote and/or accept it as solution. To your question search for cascade.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.