Search code examples
phpdb2ibm-midrange

How to get column names from a query in IBM db2 via SQL


Is it possible to get the list column names from a query in IBM db2?

Consider a very simple example I want the column names fetched by following SQL;

select * from db.t1,db.t2 where t1.id = t2.id 

Actually I know how to get column names from a a single table. But facing difficulties to get column names from such scenario.

I want the list of columns as an Array in PHP. It can be done if I just add "FETCH FIRST 1 ROW ONLY" as the end of the SQL, and run it. Then from result set I can get the columns.

But if there is no data then also I need the list of columns. How to achieve that?

Any help would be great for me.


Solution

  • You can use db2_num_fields() to determine the number of columns in the result set, then loop over them and call db2_field_name() to obtain the names.