ADODB 获取列数
我正在阅读公司的代码库,发现有些事情似乎可以做得更好。
$dbRow = $dbh->Execute("SELECT * FROM database.table LIMIT 1");
$tableColumnCount = $dbRow->_numOfFields;
这是使用 ADODB 获取列数的唯一方法吗?必须执行查询才能提出问题,这似乎很愚蠢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 INFORMATION_SCHEMA.COLUMNS 表
:也就是说,您可能需要在 INFORMATION_SCHEMA.COLUMNS 之前运行 FLUSH TABLES 命令来反映现有的表和列 因为数据被缓存了。
You can use the INFORMATION_SCHEMA.COLUMNS table:
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.无论哪种方式,您最终都必须访问数据库,除非您的程序能够神奇地知道表的架构。如果 LIMIT 0 在 MySQL 中是合法的,那么这会稍微更有效 - 正如我期望的那样,将 LIMIT 1 替换为 WHERE 1 = 0。
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.
MetaColumns 或 MetaColumnNames 应该为您提供此信息
MetaColumns or MetaColumnNames should give you this information