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

jQuerybeast

I am trying to parse all the column fields and data of a single row of any selected mysql table.

The reason behind this is to make a 'universal'-like Table parser of any given single row.

For example I have this table '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:

<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. 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'];

anurupr

if $p is a single-level array like

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

and $fields is an array like this

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

Then this should work

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Log when a row is deleted from a given table in MySQL

From Dev

Find common values (Intersection) from one column of a table depending on the given values of second column of same table

From Dev

PHP & MySQL: Get all values from a table?

From Dev

Deleting a row in grid view,in which data and column names are given from code behind

From Dev

How to create temporary table with given column names from select?

From Dev

How to get all the column of a given row of a table

From Dev

MySQL - Get column names from table and display them (using PHP)

From Dev

Display any given MySQL SELECT query as a HTML table using PHP

From Dev

Transform single table row + column names into columns

From Dev

Print all rows with a given word in a single column?

From Dev

MYSQL find all rows where second row have given values

From Dev

Log when a row is deleted from a given table in MySQL

From Dev

Find common values (Intersection) from one column of a table depending on the given values of second column of same table

From Dev

display all tables names from a given database name

From Dev

Display data from single table where column values match

From Dev

Display different rows of a record in a single row having all the column values

From Dev

HtmlAgilityPack method for returning value from a row in a table given column data

From Dev

MySQL - Get column names from table and display them (using PHP)

From Dev

MySQL / PHP - Display all rows from table except one single row

From Dev

Display any given MySQL SELECT query as a HTML table using PHP

From Dev

find a combination of numbers that equal a given sum from a single column in mysql table

From Dev

mysql how to update a column of every row with a given set of values

From Dev

Displaying Column names of empty cells for a given row

From Dev

DIsplay rows in a table by substituting value for a given column

From Dev

Group concat the column names from the table based on their row values

From Dev

Checking Given Number in From and To column in a table mysql

From Dev

Delete row and column in symmetric array if all the values in a row (or column) do not satisfy a given contion

From Dev

Given a table with a varchar column, return the entries that do not have any suffix in a different row

From Dev

How to print values from multiple table in single table row in php

Related Related

  1. 1

    Log when a row is deleted from a given table in MySQL

  2. 2

    Find common values (Intersection) from one column of a table depending on the given values of second column of same table

  3. 3

    PHP & MySQL: Get all values from a table?

  4. 4

    Deleting a row in grid view,in which data and column names are given from code behind

  5. 5

    How to create temporary table with given column names from select?

  6. 6

    How to get all the column of a given row of a table

  7. 7

    MySQL - Get column names from table and display them (using PHP)

  8. 8

    Display any given MySQL SELECT query as a HTML table using PHP

  9. 9

    Transform single table row + column names into columns

  10. 10

    Print all rows with a given word in a single column?

  11. 11

    MYSQL find all rows where second row have given values

  12. 12

    Log when a row is deleted from a given table in MySQL

  13. 13

    Find common values (Intersection) from one column of a table depending on the given values of second column of same table

  14. 14

    display all tables names from a given database name

  15. 15

    Display data from single table where column values match

  16. 16

    Display different rows of a record in a single row having all the column values

  17. 17

    HtmlAgilityPack method for returning value from a row in a table given column data

  18. 18

    MySQL - Get column names from table and display them (using PHP)

  19. 19

    MySQL / PHP - Display all rows from table except one single row

  20. 20

    Display any given MySQL SELECT query as a HTML table using PHP

  21. 21

    find a combination of numbers that equal a given sum from a single column in mysql table

  22. 22

    mysql how to update a column of every row with a given set of values

  23. 23

    Displaying Column names of empty cells for a given row

  24. 24

    DIsplay rows in a table by substituting value for a given column

  25. 25

    Group concat the column names from the table based on their row values

  26. 26

    Checking Given Number in From and To column in a table mysql

  27. 27

    Delete row and column in symmetric array if all the values in a row (or column) do not satisfy a given contion

  28. 28

    Given a table with a varchar column, return the entries that do not have any suffix in a different row

  29. 29

    How to print values from multiple table in single table row in php

HotTag

Archive