简体   繁体   English

PHP MySQL:显示给定表中的所有列名以及任何给定行的值

[英]PHP MySQL: Display all column names from a given table and the values for any single given row

I am trying to parse all the column fields and data of a single row of any selected mysql table. 我试图解析任何选定的mysql表的单行的所有列字段和数据。

The reason behind this is to make a 'universal'-like Table parser of any given single row. 其背后的原因是要对任何给定的单行制作一个类似于“通用”的表格解析器。

For example I have this table ' tbl1 ': 例如,我有此表' tbl1 ':

+----+---------------------+---------+---------+--+
| id | date                | amounta | amountb |  |
+----+---------------------+---------+---------+--+
| 1  | 2014-02-28 05:58:41 | 148     | 220     |  |
+----+---------------------+---------+---------+--+
| 2  | 2014-01-20 05:58:41 | 50      | 285     |  |
+----+---------------------+---------+---------+--+
| 3  | 2014-03-30 05:58:41 | 501     | 582     |  |
+----+---------------------+---------+---------+--+

and I want to be able to select table tbl1 and id = 1 to export into: 我希望能够选择表tbl1id = 1导出到:

<label>id <input type="text" value="1"/></label>
<label>date <input type="text" value="2014-02-28 05:58:41"/></label>
<label>amounta <input type="text" value="148"/></label>
<label>amountb <input type="text" value="220"/></label>

This is what I have thus far: 到目前为止,这是我所拥有的:

if ($_GET['p'] && $_GET['table']) {
    include ("con.php");

    $query = "SELECT * FROM `" . $_GET['table'] . "` WHERE id = '" . $_GET['p'] . "'";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $fields[] = $row['0'];
        $p = $row;
    }

    $fields = array();
    $res = mysql_query("SHOW COLUMNS FROM `" . $_GET['table'] . "`");
    while ($x = mysql_fetch_assoc($res)) {
        $fields[] = $x['Field'];
    }

    foreach($fields as $f) {
        foreach($p as $obj) {
            echo '<label>' . $f . ' <input type="text" value="' . $p[$f] . '"></label>';
        };
    }

    mysql_close();
}

The problem I'm sure is somewhere between the foreach looping. 我确定问题出在foreach循环之间。 I know its totally wrong but im not quite sure how to solve this problem. 我知道它是完全错误的,但是我不太确定如何解决这个问题。

Basically the idea is to select all column names from $_GET['table'] and for each column name find its value where id = $_GET['p'] ; 基本上,这个想法是从$ _GET ['table']中选择所有列名称,并为每个列名称找到其值,其中id = $ _GET ['p'] ;

Use mysql_fetch_field . 使用mysql_fetch_field

$fields = array();
while($f = mysql_fetch_field($query)) $fields[] = $f->name;

In this way you can get all field names, works for any kind of query rather than just SELECT * 这样,您可以获得所有字段名称,适用于任何类型的查询,而不仅仅是SELECT *

if $p is a single-level array like 如果$p是一个单层数组,例如

    Array (
            'field1' => 'value1',
            'field2' => 'value2',
            ...
          )

and $fields is an array like this $fields是这样的数组

    Array (
            0 => 'field1',
            1 => 'field2',
            ...
          )

Then this should work 然后这应该工作

foreach($fields as $f) {
       echo '<label>' . $f . ' <input type="text" value="' . $p[$f] . '"></label>';    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM