Question: I want to execute a SELECT query but I don't how many columns to select.

Like:

```
select name, family from persons;

```

How can I know which columns to select?

"I am currently designing a site for the execute query by users.
So when the user executes this query, I won't know which columns selected.
But when I want to show the results and draw a table for the user I should know which columns selected."

Answer: 
If you want to get column names for any query in all cases it's not so easy. 

In case at least one row is returned you can get columns directly from this row. 

But when you want to get column names when there is no result to display table/export to CSV, you need to use PDO functions that are not 100% reliable.

```
// sample query - it might contain joins etc.
$query = 'select person.name, person.family, user.id from persons LEFT JOIN users ON persons.id = user.person_id';

$statement = $pdo->query($query);
$data = $statement->fetchAll(PDO::FETCH_CLASS);
if (isset($data[0])) {
  // there is at least one row - we can grab columns from it
  $columns = array_keys((array)$data[0]);
}
else {
   // there are no results - no need to use PDO functions
   $nr = $statement->columnCount();
   for ($i = 0; $i < $nr; ++$i) {
      $columns[] = $statement->getColumnMeta($i)['name'];
   }
}

```
use `DESC table` or

**Example**

```
SELECT GROUP_CONCAT(column_name) FROM information_schema.columns WHERE table_name='your_table';

```

**output**

```
 column1,column2,column3

```