1

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

| 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. 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. i.e. For the Column "FirstName" the comments read "1(a) First Name". 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. It seems like a pretty simple task but can't for the life of me find relevant documented solutions.

4

3 回答 3

0

尝试这个:

(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);
于 2013-08-21T21:25:35.893 回答
0

我建议您fetch_fields()对结果集使用 mysqli 函数,以获取有关结果集中包含的列的信息,而不是在 INFORMATION_SCHEMA 中查询视图。

fetch_fields()将适用于仅引用表中列子集的查询,或返回表达式的查询,或从多个表中返回列。

http://php.net/manual/en/mysqli.quickstart.metadata.php

于 2013-08-21T21:19:19.753 回答
0

最后,解决方案。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"

所以我循环通过 fetch_fields 来获取对应的列评论,然后循环通过 answers 查询的结果来显示下一个表列的每个答案。

于 2013-08-24T23:24:13.033 回答