C
C
Cyril2020-06-01 00:38:48
PHP
Cyril, 2020-06-01 00:38:48

How to display data from two different databases in 1 cycle?

Hello, tell me how you can display information from two different databases in 1 cycle. So far I have only been able to do this with two cycles, is it possible somehow with one?

$sql_table1 = $link_s1->prepare("SELECT * FROM `bans` ORDER BY `time` DESC");
  $sql_table1->execute();
  if($sql_table1->rowCount() > 0) {
    while($table = $sql_table1->fetch(PDO::FETCH_BOTH)) {

      $tmpban = $table['expires'];
      $timeConvert_end = $tmpban / 1000;
      $bantime = $table['time'];
      $timeConvert_start = $bantime / 1000;

      echo '<tr class="find"><td>'.$table['name'].'</td>';
      echo '<td>'.$table['banner'].'</td>';
      echo '<td>'.$table['reason'].'</td>';
      echo '<td>'.Date("H:i d.m.Y", $timeConvert_start).'</td>';
      if ($tmpban == '0') {
        print '<td class="perm">Перманент</td>';
      } else {
        print '<td>'.Date("H:i d.m.Y", $timeConvert_end).'</td>';
      }
    }
  }

  $sql_table2 = $link_s2->prepare("SELECT * FROM `bans` ORDER BY `time` DESC");
  $sql_table2->execute();
  if($sql_table2->rowCount() > 0) {
    while($table = $sql_table2->fetch(PDO::FETCH_BOTH)) {

      $tmpban = $table['expires'];
      $timeConvert_end = $tmpban / 1000;
      $bantime = $table['time'];
      $timeConvert_start = $bantime / 1000;

      echo '<tr class="find"><td>'.$table['name'].'</td>';
      echo '<td>'.$table['banner'].'</td>';
      echo '<td>'.$table['reason'].'</td>';
      echo '<td>'.Date("H:i d.m.Y", $timeConvert_start).'</td>';
      if ($tmpban == '0') {
        print '<td class="perm">Перманент</td>';
      } else {
        print '<td>'.Date("H:i d.m.Y", $timeConvert_end).'</td>';
      }
    }
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adik Izat, 2020-06-01
@DERUINO

Above you are correct. You make both requests, do execute (), and then immediately fetch into a variable. It turns out two tables. If you need to exclude identical elements in tables, use array_unique() and merge. Then iterate in the loop as you would like.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question