[英]php to display table; column one with mysql column comments and column two transposed row results
Apologies for the perhaps poor title - It is best to display my goal visually: 不好意思的标题表示歉意-最好以视觉方式显示我的目标:
I want something like this to show on the page when user goes to SomeURL.com/test.php?id=101 当用户转到SomeURL.com/test.php?id=101时,我希望在页面上显示类似内容
| Questions | Answers |
----------------------------------
| 1(a) First Name| Pedro |
----------------------------------
| 1(b) Surname | Millers |
----------------------------------
| 2(a) Weight | 150lbs |
----------------------------------
This will be acheived by queries to a mysql database. 通过查询mysql数据库可以实现这一点。 Firstly: 首先:
SELECT * FROM Questionnaire WHERE idQuestionnaire=101;
Which returns: 哪个返回:
| idQuestionnaire | FirstName | Surname | Weight |
-----------------------------------------------------------
| 101 | Pedro | Millers | 150lbs |
In my table settings I have Column comments set. 在表格设置中,设置了“列注释”。 ie For the Column "FirstName" the comments read "1(a) First Name". 例如,对于“名字”列,注释显示为“ 1(a)名字”。 To retreive all of these comments I can do another query: 要检索所有这些评论,我可以执行另一个查询:
SELECT COLUMN_NAMES FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='Questionnaire';
Which returns: 哪个返回:
| COLUMN_COMMENTS |
-------------------
| 1(a) First Name |
-------------------
| 1(b) Surname |
-------------------
| 2(a) Weight |
-------------------
The reason I want to automate the code with arrays/loops rather than hard code is because the actual table I am using has over 400 fields, and fields may be modified or added so I would like not to have to keep changing the php code as well when this happens. 我想使用数组/循环而不是硬代码自动执行代码的原因是因为我使用的实际表具有400多个字段,并且可以修改或添加字段,所以我不想不必不断更改php代码,因为好吧,这发生的时候。 It seems like a pretty simple task but can't for the life of me find relevant documented solutions. 这似乎是一个非常简单的任务,但我一生无法找到相关的书面解决方案。
I'd recommend you use the mysqli fetch_fields()
function on the result set, to get information about the columns contained in the resultset, rather than querying views in INFORMATION_SCHEMA. 我建议您在结果集上使用mysqli fetch_fields()
函数,以获取有关结果集中包含的列的信息,而不是在INFORMATION_SCHEMA中查询视图。
The fetch_fields()
will work for queries that reference only a subset of columns in a table, or queries that return expressions, or return columns from more than one table. fetch_fields()
适用于仅引用表中一列列的查询,返回表达式或从多个表中返回列的查询。
http://php.net/manual/en/mysqli.quickstart.metadata.php http://php.net/manual/zh/mysqli.quickstart.metadata.php
Try this: 尝试这个:
(SELECT
"1(a) First Name" as Questions,
Firstname as Answers
FROM Questionnaire WHERE idQuestionnaire=101)
union
(SELECT
"1(b) Surname" as Questions,
Surname as Answers
FROM Questionnaire WHERE idQuestionnaire=101)
union
(SELECT
"2(a) Weight" as Questions,
Weight as Answers
FROM Questionnaire WHERE idQuestionnaire=101);
Finally, the solution. 最后,解决方案。 Spencer7593 got me on my way but the final code required is as follows: Spencer7593吸引了我,但所需的最终代码如下:
$con=mysqli_connect("localhost","user","password","test");
if (mysqli_connect_errno($con)) {echo "MySQL conn. err:".mysqli_connect_error();}
$sql = "SELECT column_comment,column_name FROM information_schema.columns
WHERE table_name = 'mytablename';";
$query = mysqli_query($con,$sql) or die(mysql_error());
$columnArray = array();
while(($result = mysqli_fetch_array($query, MYSQL_ASSOC))){
if($result['column_comment'])
{
$CurCol = $result['column_comment'];
}
else
{
$CurCol = "No text stored";
}
$CurName = $result['column_name'];
$columnArray[$CurName]=$CurCol;
}
//echo $columnArray['FirstName']; //test example to return "1(a) First Name"
So I looped through fetch_fields to get the corresponding column comments, and then looped through the result of the answers query to display each answer down the next table column. 因此,我遍历了fetch_fields以获取相应的列注释,然后遍历了答案查询的结果以在下一表格列中显示每个答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.