Mentos93 Mentos93 - 7 months ago 17
PHP Question

Display more than one column from database using php

I have a php function:

function get_files() {
global $path, $sql_algemeen, $oldServer, $newServer;
$dir = opendir($path);
while($file = readdir($dir) ) {
if(substr($file,0,1) != '.') {
$query = "SELECT * FROM rb_migration_files WHERE filename = '$file'";
$sql_algemeen->Query($query);
if ($sql_algemeen->NumberRows() > 0) {
$sql_algemeen->ReadRow();
$files[$file] = $sql_algemeen->RowData['status'];
<<<< What should I do here or how should I modify this part. >>>>
} else {
$query = "INSERT INTO rb_migration_files (filename, status, old_server, new_server) VALUES ('$file', 0, $oldServer, $newServer);";
$sql_algemeen->Query($query);
$files[$file] = 0;
}
}
}
return($files);
}


How can I retrieve more that one column from the database? Because in this case only the
$files[$file] = $sql_algemeen->RowData['status'];
is one thing that is returned, but I want to get two more columns.

And how would I echo it then? Because this is how it's done on this moment;

foreach($files as $file=>$status) {
echo '<tr>';
echo '<td>' . '<a href="?bestand='. $file.'">'.$file . '</a></td>';
echo '<td> . $status . </td>';
echo '<td style="text-align:center">'. <<Here I want the next columns>> .'</td>';
echo '<td style="text-align:center">' . <<And the next one>> .'</td>';
echo '</tr>';
}


I can feel I am close but I just don't get it with more columns.

Answer

You should be able to get data of other columns with $sql_algemeen->RowData['other_column_name'];. The code already selects all columns from the database as the SELECT * indicates (* stands for all columns).

To 'echo' the data you first need to add it to the $files array so it is available in the echo loop. For this you need to rewrite the $files array. Here a rewrite for your code with the new column "some_column":

function get_files() {
  global $path, $sql_algemeen, $oldServer, $newServer;
  $dir = opendir($path);
  while($file = readdir($dir) ) {
      if(substr($file,0,1) != '.') {
          $query = "SELECT * FROM rb_migration_files WHERE filename = '$file'";
          $sql_algemeen->Query($query);
          if ($sql_algemeen->NumberRows() > 0) {
              $sql_algemeen->ReadRow();
              $files[$file] = array(
                'status' => $sql_algemeen->RowData['status'],
                'some_column' => $sql_algemeen->RowData['some_column'],
              );
          } else {
              $query = "INSERT INTO rb_migration_files (filename, status, old_server, new_server) VALUES ('$file', 0, $oldServer, $newServer);";
              $sql_algemeen->Query($query);
              $files[$file] = array(
                'status' => 0,
                'some_column' => 'some_value', // whatever the columns value is on creation.
              );
          }
      }
  }
  return($files);
}

foreach($files as $file=>$column) {
  echo '<tr>';
  echo '<td>' . '<a href="?bestand='. $file.'">'.$file . '</a></td>';
  echo '<td> . $column['status'] . </td>';
  echo '<td> . $column['some_column'] . </td>';
  echo '<td style="text-align:center">'. <<Here I want the next columns>> .'</td>';
  echo '<td style="text-align:center">' . <<And the next one>> .'</td>';
  echo '</tr>';
}

Of course changing the $files variable could break other parts of your code that you have not mentioned here.