如何在另一个内部使用一个stmt查询

i have to get data from one table from my database, BUT after getting the data, i have to access another table to get more data using the id found in the first query.

Here is my code:

$query = "SELECT id,name,datetime FROM table1 WHERE id=?";

if($stmt=mysqli_prepare($mysqli,$query)){
    mysqli_stmt_bind_param($stmt,"i",$_SESSION['id']);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt,$id,$name,$datetime);
    while(mysqli_stmt_fetch($stmt)){
        $query2 = "SELECT id FROM table2 WHERE id=?";
        if($stmt2=mysqli_prepare($mysqli,$query2)){
            mysqli_stmt_bind_param($stmt2,"s",$id2);
            mysqli_stmt_execute($stmt2);
            mysqli_stmt_store_result($stmt2);
            $num = mysqli_stmt_num_rows($stmt2);
        }

The code does not work, i know i can't do that. I'm new with mysqli, in MySQL it works, but in MySQLi don't.