Welcome Guest, Not a member yet? Register   Sign In
Count number of columns retrieved from database query
#1

[eluser]Jason Tan Boon Teck[/eluser]
I'm trying to write a report generator, that makes use of different queries generated from the model, based on what the user chooses. As such the number of columns for these ad hoc views are different.

Counting rows is easy. Is there a way to count the number of columns returned? The queries are complex SQL statements that contains joins. So I cannot use the SQL command to query the table structure.

btw, I'm using PostgreSQL.

TIA. Jason Tan
#2

[eluser]mddd[/eluser]
From the CI manual:

$query->num_fields()
The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:
Code:
$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_fields();
#3

[eluser]Jason Tan Boon Teck[/eluser]
[quote author="mddd" date="1275319497"]From the CI manual:

$query->num_fields()
The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:
Code:
$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_fields();
[/quote]

I never thought I would ever use this method, when I read the manual long ago.

Thank you for the pointer.




Theme © iAndrew 2016 - Forum software by © MyBB