54

How can I get all the column names from a table using PDO?

id         name        age
1          Alan        35      
2          Alex        52
3          Amy         15

The info that I want to get are,

id         name        age

EDIT:

Here is my attempt,

$db = $connection->get_connection();
$select = $db->query('SELECT * FROM contacts');

$total_column = $select->columnCount();
var_dump($total_column);

for ($counter = 0; $counter < $total_column; $counter ++) {
    $meta = $select->getColumnMeta($counter);
    $column[] = $meta['name'];
}
print_r($column);

Then I get,

Array
(
    [0] => id
    [1] => name
    [2] => age
    ...

)
5
  • 6
    Do you want the column names in a table, or the column names in the record set from a query? These are two different things done in two different ways. Phil's answer does the former, JapanPro's answer does the latter! Commented Mar 25, 2011 at 3:37
  • @charles: I think I only want to get the column names in a table. I don't quite understand what u mean by column names in the record set from a query. but see my answer in my edit above. thanks. Commented Mar 25, 2011 at 4:09
  • @lauthiamkok: You're doing that in your updated post -- you're making a query, which returns a set of records, then you're grabbing the column names for that specific set. It just happens that the column names in your result set are identical to those in your table. Remember, SQL columns can be aliased. Commented Mar 25, 2011 at 4:13
  • 1
    @lauthiamkok, if you were trying to get the column names from a table, then @JapanPro's answer below is the best way to do that -- using the information_schema method. What you are doing does the job but isn't the "right" way. Commented Mar 25, 2011 at 4:26
  • 2
    when you have an empty table your method fails, because there's no records to fetch the columns from. Commented Dec 15, 2011 at 21:32

12 Answers 12

98

I solve the problem the following way (MySQL only)

$table_fields = $dbh->query("DESCRIBE tablename")->fetchAll(PDO::FETCH_COLUMN);

$dbh means "database handle" and it represents the connection object.

Sign up to request clarification or add additional context in comments.

4 Comments

This is only supported by MySQL.
why not $q = $dbh->query("DESCRIBE tablename"); $table_fields = $q->fetchAll(PDO::FETCH_COLUMN);?
Reader, for a generic solution, see @Will answer.
IMHO, while this answer may be useful to many, it doesn't match the need expressed in the question, i.e. "Is there a way to retrieve the column name from the rowset array while I'm iterating data?" The user wants to see the key in the array while iterating values. The key($array) function may help.
45

This will work for MySQL, Postgres, and probably any other PDO driver that uses the LIMIT clause.

Notice LIMIT 0 is added for improved performance:

$rs = $db->query('SELECT * FROM my_table LIMIT 0');
for ($i = 0; $i < $rs->columnCount(); $i++) {
    $col = $rs->getColumnMeta($i);
    $columns[] = $col['name'];
}
print_r($columns);

9 Comments

Thanks Will! Hello reader, that is the generic PDO solution! The Lauer solution only supported by MySQL -- and PDOStatement::getColumnMeta() is complex, is not supported for all PDO drivers, and is listed as experimental. PS: the query runs also with empty tables.
Perhaps simplify with something like $columns = array_keys($rs->fetchAll(PDO::FETCH_ASSOC));... I not try.
@PeterKrauss, yeah, it needs at least one result.
@DaviddCeFreitas, sorry the method getColumnMeta have a 6+ years old "is EXPERIMENTAL" alert...The only way is using a non-empty table. You can use something like SELECT * FROM my_table WHERE id=0 and populate all tables with a no-data row.
LIMIT 0 is a MySQL / PGSQL specific syntax. How is this generic? How to make it so?
|
23

My 2 cents:

$result = $db->query('select * from table limit 1');
$fields = array_keys($result->fetch(PDO::FETCH_ASSOC));

And you will get the column names as an array in the var $fields.

1 Comment

This produces a Warning if the table is empty! ===> "Warning: array_keys() expects parameter 1 to be array, bool given in..."
16

A simple PHP function

function getColumnNames($pdo, $table) {
    $sql = "SELECT column_name FROM information_schema.columns WHERE table_name = ?";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$table]);
    return $stmt->fetchAll(PDO::FETCH_COLUMN);
}

9 Comments

add also schema_name to where
+1 for information_schema instead of something DB-specific.
@Charles Isn't information_schema MySQL specific? This question isn't tagged MySQL so it could be anything. Mind you, the method in my answer probably doesn't work on every DB either ;)
@Phil, information_schema is part of the ANSI standard. It's been implemented in MySQL, PostgreSQL and MSSQL. Unless the OP is using Oracle or SQLite, it should work fine for him (assuming this answer was what he wanted).
@PawełStawarz there is a TABLE_SCHEMA column that indicates what schema the table and column belong to. However, the downsides to using information_schema is that it is possible not all users/accounts will have access to this system table. This is also slower than getColumnMeta().
|
2

Here is the function I use. Created based on @Lauer answer above and some other resources:

//Get Columns
function getColumns($tablenames) {
global $hostname , $dbnames, $username, $password;
try {
$condb = new PDO("mysql:host=$hostname;dbname=$dbnames", $username, $password);

//debug connection
$condb->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$condb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// get column names
$query = $condb->prepare("DESCRIBE $tablenames");
$query->execute();
$table_names = $query->fetchAll(PDO::FETCH_COLUMN);
return $table_names;

//Close connection
$condb = null;

} catch(PDOExcepetion $e) {
echo $e->getMessage();
}
}

Usage Example:

$columns = getColumns('name_of_table'); // OR getColumns($name_of_table); if you are using variable.

foreach($columns as $col) {
echo $col . '<br/>';
}

Comments

2

This is an old question but here's my input

function getColumns($dbhandle, $tableName) {
    $columnsquery = $dbhandle->query("PRAGMA table_info($tableName)");
    $columns = array();
    foreach ($columnsquery as $k) {
        $columns[] = $k['name'];
    }
    return $columns;
}

just put your variable for your pdo object and the tablename. Works for me

Comments

2

This approach works for me in SQLite and MySQL. It may work with others, please let me know your experience.

  • Works if rows are present
  • Works if no rows are present (test with DELETE FROM table)

Code:

$calendarDatabase = new \PDO('sqlite:calendar-of-tasks.db');    
$statement = $calendarDatabase->query('SELECT *, COUNT(*) FROM data LIMIT 1');
$columns = array_keys($statement->fetch(PDO::FETCH_ASSOC));
array_pop($columns);
var_dump($columns);

I make no guarantees that this is valid SQL per ANSI or other, but it works for me.

Comments

1

PDOStatement::getColumnMeta()

As Charle's mentioned, this is a statement method, meaning it fetches the column data from a prepared statement (query).

1 Comment

And, as @Darragh mentioned, it is experimental and may not work in future.
1

I needed this and made a simple function to get this done.

function getQueryColumns($q, $pdo){
    $stmt = $pdo->prepare($q);
    $stmt->execute();
    $colCount = $stmt->columnCount();
    $return = array();
    for($i=0;$i<$colCount;$i++){
        $meta = $stmt->getColumnMeta($i);
        $return[] = $meta['name'];
    }
    return $return;
}

Enjoy :)

Comments

0

A very useful solution here for SQLite3. Because the OP does not indicate MySQL specifically and there was a failed attempt to use some solutions on SQLite.

    $table_name = 'content_containers';
    $container_result = $connect->query("PRAGMA table_info(" . $table_name . ")");
    $container_result->setFetchMode(PDO::FETCH_ASSOC);


    foreach ($container_result as $conkey => $convalue)
    {

        $elements[$convalue['name']] = $convalue['name'];

    }

This returns an array. Since this is a direct information dump you'll need to iterate over and filter the results to get something like this:

Array
(
    [ccid] => ccid
    [administration_title] => administration_title
    [content_type_id] => content_type_id
    [author_id] => author_id
    [date_created] => date_created
    [language_id] => language_id
    [publish_date] => publish_date
    [status] => status
    [relationship_ccid] => relationship_ccid
    [url_alias] => url_alias
)

This is particularly nice to have when the table is empty.

Comments

0

My contribution ONLY for SQLite:

/**
 * Returns an array of column names for a given table.
 * Arg. $dsn should be replaced by $this->dsn in a class definition.
 *
 * @param string $dsn Database connection string, 
 * e.g.'sqlite:/home/user3/db/mydb.sq3'
 * @param string $table The name of the table
 * 
 * @return string[] An array of table names
 */
public function getTableColumns($dsn, $table) {
   $dbh = new \PDO($dsn);
   return $dbh->query('PRAGMA table_info(`'.$table.'`)')->fetchAll(\PDO::FETCH_COLUMN, 1);
}

Comments

-1

Just Put your Database name,username,password (Where i marked ?) and table name.& Yuuppiii!.... you get all data from your main database (with column name)

<?php 

function qry($q){

    global $qry;
    try {   
    $host = "?";
    $dbname = "?";
    $username = "?";
    $password = "?";
    $dbcon = new PDO("mysql:host=$host; 
    dbname=$dbname","$username","$password");
}
catch (Exception $e) {

    echo "ERROR ".$e->getMEssage();

}

    $qry = $dbcon->query($q);
    $qry->setFetchMode(PDO:: FETCH_OBJ);

    return $qry;

}


echo "<table>";

/*Get Colums Names in table row */
$columns = array();

$qry1= qry("SHOW COLUMNS FROM Your_table_name");

while (@$column = $qry1->fetch()->Field) {
    echo "<td>".$column."</td>";
    $columns[] = $column;

}

echo "<tr>";

/* Fetch all data into a html table * /

$qry2 = qry("SELECT * FROM Your_table_name");

while ( $details = $qry2->fetch()) {

    echo "<tr>";
    foreach ($columns as $c_name) {
    echo "<td>".$details->$c_name."</td>";

}

}

echo "</table>";

?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.