Tutorial :ADODB Getting the column count



Question:

I was reading through our code base at my company and I saw something that seemed like it could be done better.

$dbRow = $dbh->Execute("SELECT * FROM database.table LIMIT 1");  $tableColumnCount = $dbRow->_numOfFields;  

Is this the only way to get a column count using ADODB? It just seems silly to have to execute a query so you can ask the question.


Solution:1

You can use the INFORMATION_SCHEMA.COLUMNS table:

SELECT COUNT(*)     FROM INFORMATION_SCHEMA.COLUMNS   WHERE table_name = 'your_table_name'     AND table_schema = 'your_database_name'  

That said, you might want to run the FLUSH TABLES command prior to for the INFORMATION_SCHEMA.COLUMNS to reflect existing tables and columns because the data is cached.


Solution:2

Whichever way, you'll have to end up hitting the database unless your program can magically know the schema of the table. If LIMIT 0 is legal in MySQL then this would be slightly more efficient - as I expect would replacing LIMIT 1 with WHERE 1 = 0.


Solution:3

MetaColumns or MetaColumnNames should give you this information


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »