0

I need to combine two MySQL result variables since I have two different hosts and union all function cannot use in the query. Here is my attempt. First I retrieved the results as below:

$stmt_ch = $conn_1->prepare("SELECT ts,number from blocklist where number like ?");
$stmt_ch->bind_param('s', $num_remove);
$stmt_ch->execute();
$result_ch = $stmt_ch->get_result();

$stmt_ch2 = $conn_2->prepare("SELECT ts,number from blocklist where number like ?");
$stmt_ch2->bind_param('s', $num_remove);
$stmt_ch2->execute();
$result_ch2 = $stmt_ch2->get_result();

Var_dump( $result_ch) gives me an array, so I decided to merge these two arrays as below. Maybe I am wrong.

$result_ch_com = array_merge($result_ch, $result_ch2);

Then tried to use as below:

if($result_ch_com->num_rows > 0){

-------

}

Seems this is wrong too. Maybe I am completely wrong, I have no idea. My intention is to show all the results in one table. Can someone help me on this?

I have already checked this thread, Combine two arrays, to get an idea to combine arrays but it didn't work for me

Update:

  if(count($result_ch_com) > 0){
    
        $output .= '
<table class="table table-bordered">
                    <tr>
                    <th width="25%" style="text-align:center">Blocked Time</th>
                    <th width="20%" style="text-align:center">Blocked Number</th>
                    </tr>
';
    
foreach($result_ch_com as $row) {
       $output .= '
    <tr>
    <td style="text-align:center">'.$row['ts'].'</td>
    <td style="text-align:center">'.$row['number'].'</td>
    </tr>
    '; 
    }

$output .= '</table>';  
echo $output;
    
}

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ShEi
  • 55
  • 7
  • How come Var_dump( $result_ch) gives you an array? – Your Common Sense Aug 29 '22 at 17:26
  • I got this type of output..object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(2) ["type"]=> int(0) } – ShEi Aug 29 '22 at 17:30
  • So it says *object* not array. Besides, it doesn't contain anything like your data, does it? – Your Common Sense Aug 29 '22 at 17:33
  • mm..yeah..Seems I am wrong from the beginning... But the suggested duplicate thread is also wrong and my question also closed.. Pathetic – ShEi Aug 29 '22 at 17:35
  • 1
    just get 2 arrays from your queries and combine them. What's wrong with it? If you don't know how to get an array from mysql query, you should ask this first (it will be also a duplicate tho). – Your Common Sense Aug 29 '22 at 17:37

1 Answers1

0

Unfortunately get_result doesn't return an array. You can however use the result and get an array out of that.

$result_ch_com = array_merge(
    mysqli_fetch_all($result_ch, MYSQLI_NUM),
    mysqli_fetch_all($result_ch2, MYSQLI_NUM)
);

if(count($result_ch_com) > 0){
    foreach($result_ch_com as $row) {
        echo $row['ts'];
    }
}

Sources: mysqli_result class & mysqli_fetch_all method.

  • @thomasberends, Var_dump provided all the results I needed.Seems I am doing wrong when I add them to table.. I have updated the question.Can you Please check and comment – ShEi Aug 29 '22 at 18:42
  • @ShEi I've adjusted my answer. To loop through the results, you now need to use foreach. The way you loop is only for database results, while this is now an array. I've provided an example so you can expand it to fit your needs. – thomasberends Aug 29 '22 at 18:50
  • 1
    why there is if(count($result_ch_com) > 0){ ? What would change if I remove it? – Your Common Sense Aug 29 '22 at 19:15
  • @thomasberends I have updated the question with the answer you provided. DId i made any wrong? – ShEi Aug 30 '22 at 07:13