How to write all columns of an SQL table + php

Asked

Viewed 49 times

1

Good afternoon, I’m having a doubt, I wanted to create an echo that showed all columns with a select, my code is as follows

<?php
  $db = 'localhost:F:\Dados\TGA.FDB';
  $username = 'SYSDBA';
  $password = 'masterkey';
  // Connect to database
  $dbh = ibase_connect($db, $username, $password);
  $sql = 'SELECT * FROM TMOV WHERE IDMOV = 1974';
  // Execute query
  $rc = ibase_query($dbh, $sql);
  // Get the result row by row as object
  while ($row = ibase_fetch_object($rc)) {
    echo $row -> CODCFO, "\n";
    echo $row -> NUMEROMOV, "\n";


  }
  // Release the handle associated with the result of the query
  ibase_free_result($rc);
  // Release the handle associated with the connection
  ibase_close($dbh);
 ?>         

So instead of using two echo there’s some way I can use a loop he can repeat all columns like

for( new colunas; colunas < MAX_COLUNAS; i++){
  echo row[i];
 }

I started programming now in php and I’m still not used to all syntax

  • Is that so? https://teamtreehouse.com/library/combining-strings

1 answer

0


You can use a foreach:

  while ($row = ibase_fetch_object($rc)) { 
    foreach($row as $coluna) {
     echo $coluna;
    }
  }

In case you need to also put the column name followed by the value, for example:

echo NOMEDACOLUNA1: valordacoluna1
echo NOMEDACOLINA2: valordacoluna2...

You can also use foreach:

while ($row = ibase_fetch_object($rc)) { 
    foreach($row as $nomeColuna => $valorColuna) {
     echo $nomeColuna .": ". $valorColuna; // "NOMECOLUNAx: valorcolunax"
    }
  }
  • It worked, thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.