How to find out if parent have children in php mysql

It have two table parent table and children table
Parent Table

id page-title page-content

Children Table (where parent_id = id) id from parent table

child_id parent_id child-title child-content
<?php
//parent table
$data = $conn->query("SELECT * FROM parent_page ORDER BY id DESC");
while($rows = $data->fetch(PDO::FETCH_OBJ) ):
    $parent_id = $rows->id;
?>

<?php 
//chlidren table
$child_data= $conn->query("SELECT * FROM children_page WHERE parent_id = $parent_id ");
while($child_rows = $child_data->fetch(PDO::FETCH_OBJ) ):
     $child_rows->parent_id;
?>
<?php
//requirement check if parent have children (how to check if parent have children or not)
if('parent have children'){
    then echo children content;
}else{
    echo parent content;
}
?>

<?php endwhile; endwhile;?>

Now how to check if parent have children or not if parent have children then display children content, if parent doesn’t have any children then display parent content

Solution By Mr. Barmar

<?php
$child_data = $conn->prepare("SELECT * FROM children_page WHERE parent_id = :parent");
$child_data->bindParam(':parent', $parent_id);
//parent table
$data = $conn->query("SELECT * FROM parent_page ORDER BY id DESC");
while($rows = $data->fetch(PDO::FETCH_OBJ) ):
    $parent_id = $rows->id;
?>

<?php 
    //chlidren table
    $child_data->execute();
    if ($child_data->rowCount()) {
        // parent has children
        while($child_rows = $child_data->fetch(PDO::FETCH_OBJ) ): 
            // echo children content
            ?>
<?php endwhile;
}else{
    echo parent content;
}
?>

<?php endwhile;?>

It is not efficient to execute queries inside of loops, due to the communications involved. For simple queries, it takes more time to communicate forth and back between php and the database server, then it takes to execute the query.

You would instead execute one LEFT JOIN query, between the parent table and the child table. You would then index/pivot the data using both the parent id and the child id when you fetch the data. When there is no child data, you will get a null (false) value for the 2nd index. You can test for this when you are looping over the data to produce the output.

Example, using made up data that the query would return -

// typical query
$sql = "SELECT p.id, p.content, c.id child_id, c.content child_content
	FROM parent_page p
	LEFT JOIN children_page c ON p.id = c.parent_id
	ORDER BY p.id DESC";

// make up some data
$stmt = [];
// for rows that only have parent content, there will only be one row and the child_id/child_content will be null (false)
// for rows with child content, there will be one or more rows and the child_id will be true
$stmt[] = ['id'=>'1','content'=>'p1','child_id'=>null,'child_content'=>null];
$stmt[] = ['id'=>'2','content'=>'p2','child_id'=>'1','child_content'=>'p2c1'];
$stmt[] = ['id'=>'3','content'=>'p3','child_id'=>'2','child_content'=>'p3c1'];
$stmt[] = ['id'=>'3','content'=>'p3','child_id'=>'3','child_content'=>'p3c2'];

// index/pivot the data using both the parent id and child_id
$data = [];
foreach($stmt as $row)
{
	$data[ $row['id'] ][ $row['child_id'] ] = $row;
}

// examine the data
echo '<pre>'; print_r($data); echo '<pre>';

// produce the output
foreach($data as $arr)
{
	// if the key() of the first element in $arr is false, there's only parent data
	// else, there is child data
	if(!key($arr))
	{
		// parent data only
		$row = current($arr);
		echo "Parent:{$row['content']}<br>";
	}
	else
	{
		// child data
		foreach($arr as $row)
		{
			echo "Child:{$row['content']}-{$row['child_content']}<br>";
		}
	}
}

You might want to have a read of this SitePoint article: