MySQL:通过在html页面上获取TABLE_NAME olny(无列信息)来显示表中所有数据的最简单方法
名为“db2011”的 MySQL 数据库有多个表。
使用php变量$tablename(表名正确,该表存在于db中),如何在html页面上显示“db2011”.$tablename中的数据?
是否可以通过仅执行 1 个查询来完成 - 按 $tablename 选择所有数据?
我认为可以分两步完成,但我要求任何更好的解决方案(以防万一这个不好):
Step1:通过执行获取列名称 "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='db2011' AND TABLE_NAME='".$tablename."'";
第 2 步:使用 SELECT + 以逗号分隔的列列表 FROM $tablename 构建并执行查询?
PS 我知道在 html 页面上显示的数据可能会很大。我将其限制为 100 行。
还有更好的办法吗?
谢谢。
MySQL db named "db2011" has several tables.
Using php variable $tablename (the tablename is correct, the table exists in the db), how to display the data from "db2011".$tablename on the html page?
Can it be done by performing just 1 query - to select all data by $tablename?
I think it can be done in 2 steps, but I'm asking any better solution (in case if this one is not good):
Step1: Get column names by performing
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='db2011' AND TABLE_NAME='".$tablename."'";Step2: Build and perform a query with SELECT + list of columns separated by comma FROM $tablename?
P.S. I know the data could be huge to be displayed on the html page. I will limit it by 100 rows.
Any better ways?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您正在使用 PHP 执行此操作。它可能不太优雅,但它可以在一个查询中得到它。我认为您想要在一个查询中显示表列以及数据。
I am assuming you are doing this in PHP. It may not be elegant, but it gets it in one query. I think you want to display the table columns as well as the data in one query.
为什么不直接
SELECT * FROM $tablename limit 100;
?您将在结果集中返回所有列名称。除非您还需要网页上的列类型,否则我会这样做
Why not just
SELECT * FROM $tablename limit 100;
?You would get all the column names back in the result set. Unless you also need the column type on your webpage I would go with just that