PHP PDOStatement::getColumnMeta
last modified April 19, 2025
The PDOStatement::getColumnMeta method retrieves metadata for a column in a result set. It provides information about column name, type, and other details.
Basic Definition
PDOStatement::getColumnMeta returns an associative array containing metadata about a column in a result set. The method accepts a zero-based column index.
Syntax: PDOStatement::getColumnMeta(int $column)
. Returns an array
of metadata or false on failure. Not all drivers support this method.
Basic Column Metadata
This example shows how to get basic metadata for the first column in a result set.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->query('SELECT id, name, email FROM users LIMIT 1'); $meta = $stmt->getColumnMeta(0); echo "Column 0 Metadata:\n"; print_r($meta); } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This retrieves metadata for the first column (index 0) in the result set. The output includes column name, native_type, flags, table, and other information.
Getting All Columns Metadata
This demonstrates how to get metadata for all columns in a result set.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->query('SELECT id, name, email, created_at FROM users LIMIT 1'); $columnCount = $stmt->columnCount(); for ($i = 0; $i < $columnCount; $i++) { $meta = $stmt->getColumnMeta($i); echo "Column $i: {$meta['name']} ({$meta['native_type']})\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This loops through all columns using columnCount() and gets metadata for each. It displays each column's name and native data type from the database.
Checking Column Flags
This example shows how to check column flags like primary key or not null.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->query('SELECT id, name FROM users LIMIT 1'); $meta = $stmt->getColumnMeta(0); if (in_array('primary_key', $meta['flags'])) { echo "Column {$meta['name']} is a primary key\n"; } if (in_array('not_null', $meta['flags'])) { echo "Column {$meta['name']} cannot be null\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This checks the flags array in the column metadata to determine if the column is a primary key or has a NOT NULL constraint. Different drivers may return different flags.
Getting Table Name for Column
This demonstrates how to get the source table name for a result set column.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->query('SELECT u.id, u.name, p.title FROM users u JOIN posts p ON u.id = p.user_id'); $meta = $stmt->getColumnMeta(2); if (isset($meta['table'])) { echo "Column {$meta['name']} comes from table {$meta['table']}\n"; } else { echo "Table information not available\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This gets the table name for the third column in a JOIN query result. Note that not all database drivers may provide table information in the metadata.
Handling Unsupported Drivers
This shows how to handle cases where getColumnMeta is not supported.
<?php declare(strict_types=1); try { $pdo = new PDO('sqlite::memory:'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)'); $stmt = $pdo->query('SELECT * FROM test'); $meta = $stmt->getColumnMeta(0); if ($meta === false) { echo "getColumnMeta not supported by SQLite driver\n"; } else { print_r($meta); } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This tests if getColumnMeta is supported by the SQLite driver. The method returns false for unsupported drivers. Always check the return value.
Working with Different Data Types
This example examines how different data types appear in column metadata.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->query('SELECT id, name, salary, created_at FROM users LIMIT 1'); for ($i = 0; $i < $stmt->columnCount(); $i++) { $meta = $stmt->getColumnMeta($i); echo "{$meta['name']}: {$meta['native_type']} (len: {$meta['len']})\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This displays the native type and length for each column. Different database systems may report types differently (e.g., INT vs INTEGER).
Building a Dynamic Result Processor
This shows how to use column metadata to process results dynamically.
<?php declare(strict_types=1); function processResults(PDOStatement $stmt): void { $columnCount = $stmt->columnCount(); $columns = []; // Get metadata for all columns for ($i = 0; $i < $columnCount; $i++) { $columns[$i] = $stmt->getColumnMeta($i); } // Process each row while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { foreach ($row as $col => $value) { $index = array_search($col, array_column($columns, 'name')); $type = $columns[$index]['native_type']; echo "{$col} ({$type}): " . htmlspecialchars($value) . "\n"; } echo "---\n"; } } try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->query('SELECT id, name, email FROM users LIMIT 5'); processResults($stmt); } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This function uses column metadata to process results without knowing the structure in advance. It displays each column's name, type, and value.
Best Practices
- Check Support: Verify driver supports getColumnMeta.
- Error Handling: Always check for false return value.
- Column Indexes: Remember they are zero-based.
- Performance: Cache metadata if used repeatedly.
- Driver Differences: Be aware of varying implementations.
Source
PHP getColumnMeta Documentation
This tutorial covered the PDOStatement::getColumnMeta method with practical examples showing how to retrieve and use column metadata in PHP applications.
Author
List all PHP PDO Functions.