Reputation: 93
I want to upgrade my code from mysql to pdo. When I am changing mysql_name_field to equivalent pdo. I stuck off. Check my old code.
This is in manage data class:
$result=$this->query($query);
$x=$this->cols_count($result);
$heading[]=$this->column_name($result);
print_r($heading);die();
and column_name function in data class. Manage data class inherits data class. See the column name function.
function column_name($result) {
$row=$this->stmt->getColumnMeta($result);
return $row;
}
it returns only one column name while I want all column names from the table.
Upvotes: 2
Views: 4764
Reputation: 655
The getColumnMeta() returns metadata for a column in a result set (the 0-indexed column from your result). Also, from the documentation:
Warning This function is EXPERIMENTAL. The behaviour of this function, its name, and surrounding documentation may change without notice in a future release of PHP. This function should be used at your own risk.
Warning Not all PDO drivers support PDOStatement::getColumnMeta()
What makes it not a very good approach. Instead, you can use query "SHOW COLUMNS from table" and retrieve all columns information. Alternatively, if you still want to use the getColumnMeta()
:
function column_name() {
$results = $this->query('SELECT * FROM table LIMIT 0');
for ($i = 0; $i < $results->columnCount(); $i++) {
$col = $results->getColumnMeta($i);
$columns[] = $col['name'];
}
return columns;
}
Not able to test now, but I think it will give some idea. Hope it helps,
Upvotes: 5