Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the column type with PDO?

I need a way to determine the type of a database column (varchar/numeric/date/...) when reading from the DB with PDO.

When fetching values from the DB, PDO produces only string values, regardless of the actual type of the table column.

Is there any non driver specific way to get this information? I know that there are SQL statements that retrieve the types for any given table but i'd prefer a more generic solution.

EDIT: PDOStatement::getColumnMeta() is of no use to me, because it's not supported by the PDO driver I use at the moment (Oracle).

like image 766
selfawaresoup Avatar asked Jul 01 '10 12:07

selfawaresoup


People also ask

How do I find the number of rows in PDO?

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of matching rows.

What does PDO FETCH return?

By default, PDO returns each row as an array indexed by the column name and 0-indexed column position in the row. To request a different return style, specify one of the PDO::FETCH_* constants as the first parameter when you call the PDOStatement::fetch method: PDO::FETCH_ASSOC.


1 Answers

Take a look at this method: PDOStatement->getColumnMeta

like image 144
quantumSoup Avatar answered Nov 15 '22 01:11

quantumSoup