0

i have 2 array from mysqli_fetch_assoc from 2 different db from 2 different server. my main secondary server are federated disabled and root disabled by my office so i decide to call manuall via php fetch assoc.. here is my result in table

NO. USERID  USERNAME    USERSTATUS  other   NAME
1   1   daffa   1   2   Sugeng
2   2   meito   1   4   Sugeng lagi

NO. USERID USERNAME USERSTATUS row is from $new_array1 other NAME is from $new_array2

then here is my code

$i = 1;
while(($new_array1 = mysqli_fetch_assoc( $result1)) && ($new_array2 = mysqli_fetch_assoc( $result2))){
  echo '
      <tr>
          <td>'.$i.'</td>
          <td>'.$new_array1['userid'].'</td>
          <td>'.$new_array1['username'].'</td>
          <td>'.$new_array1['userstatus'].'</td>
          <td>'.$new_array2['other'].'</td>
          <td>'.$new_array2['NAME'].'</td>
      </tr>
  ';
  $i++;
}

but there is still 2 array, how to combine 2 array from fetch assoc ? i wanna make it single array then while it to show the data

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    This sounds like a `JOIN` if that data is related. If not, you have to merge them using PHP. – tadman Nov 19 '20 at 04:48
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era and should not be used in new code. Additionally the procedural interface has less rigorous error checking and reporting, frustrating debugging efforts. – tadman Nov 19 '20 at 04:49
  • its different db on different server, join cant be done. alse federated engine is disabled by office, anyway to combine those array ? – Daffa Wardhana Nov 19 '20 at 05:02
  • If it's a whole different server you're stuck doing your own in-PHP pseudo-join. Some ORMs can handle this for you. `mysqli` cannot. – tadman Nov 19 '20 at 05:03
  • how do that with orm instead mysqli? im sure theres can do with array assoc from fetching – Daffa Wardhana Nov 19 '20 at 05:11
  • 1
    You have to fetch all rows in both sets and then stitch them together through some kind of mapping. A cheap way to do this is to convert it into an associative array keyed by the thing you're matching so you can do `$a[$key]` and `$b[$key]` and they'll link up. – tadman Nov 19 '20 at 05:12
  • 1
    by the way, what's your criteria of merging two different tables coming from two different sources anyways? just straight up align them based on row numbers? well you can't do it that way with the code above, you'd need to fetch both separately and combine results via php. – Kevin Nov 19 '20 at 05:58
  • probably related https://stackoverflow.com/a/2048085/3859027 and this one https://stackoverflow.com/a/11132606/3859027 – Kevin Nov 19 '20 at 05:58

0 Answers0