How to select all the columns in the database

0

How to select all the columns in the database with the names 'name' and 'date' I have the code

<?php
    $sql = "??";
    if ($rs = $bd->query($sql)) {
      while ($row = $rs->fetch_assoc()) {
        echo $row['data'];
        echo $row['nome'];
      }
    }
    ?>

I need to select columns with these names in all tables. I do not know if this is possible, or am I going to have to do this one by one?

    
asked by anonymous 23.09.2016 / 03:27

1 answer

2

You can do SELECT by selecting only the date and name fields:

SELECT nome,data FROM sua_tabela

And you can also select all the columns of the table

SELECT * FROM sua_tabela

If you want to select these same columns from all the tables that you have in the database and display all the results at once, try the following:

PDO

$query = $bd->query("SHOW TABLES");
while ($result = $query->fetch(PDO::FETCH_NUM)){

  $tabela = $result[0];

  $sql = "SELECT nome, data FROM ".$tabela;
  if ($rs = $bd->query($sql)) {
      while ($row = $rs->fetch_assoc()) {
        echo $row['data'];
        echo $row['nome'];
      }
    } 
  echo '<hr>';//Separa o resultado de cada coluna selecionada
}

MySQLi

$query = mysqli_query($this->conn, "SHOW TABLES");
while($result = mysqli_fetch_array($query)){

  $tabela = $result[0];

  $sql = mysqli_query("SELECT nome,data FROM ".$tabela);

  while($row = mysqli_fetch_array($sql)){

    echo $row['data'];
    echo $row['nome'];
  }
}
    
23.09.2016 / 03:29