0

I want to extract all records from PostgreSQL DB table and display those data using table in HTML without specifying the column name. I have many tables with many columns, so I want my query in HTML to be as responsive as it can.

Using this code below, I'm able to query the data from my database and print the data on browser, but I have to specify which column that I want to display.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8"> 
<title>Map</title>
</head>

<body onload="init()">
<h1>Map</h1>

<div>
<?php
$conn = pg_connect("host=localhost port=5432 dbname=visualization user=postgres password=*******");
$result = pg_query($conn,"SELECT * FROM myTable");
echo "<table>";
while($row=pg_fetch_assoc($result)){
    echo "<tr>";
    echo "<td align='center' width='200'>" . $row['lon'] . "</td>";  // set col manually
    echo "<td align='center' width='200'>" . $row['lat'] . "</td>";  // set col manually
    echo "</tr>";
}
echo "</table>";
pg_close($conn);
?>
</div>
</body>
</html>

Here's the result :

enter image description here

I only display 2 columns because I specified the column manually. I want to display all columns in the table with its records without specifying it manually (up to 100 columns). How to achieve that?

2
  • Have a look at pg_field_name in the PHP manual - it shows an example which might be of interest - php.net/manual/en/function.pg-field-name.php Commented Mar 3, 2020 at 7:14
  • One thing to look at is that if you have tables with 100 columns, it may be worth checking your database design in case there is some normalization which needs to be done. Commented Mar 3, 2020 at 7:15

3 Answers 3

1

As your columns are in an array, you can just foreach() over the row. The display of the columns will either need to be good enough or just left as defaults to make the display look reasonable though...

while($row=pg_fetch_assoc($result)){
    echo "<tr>";
    foreach ( $row as $key => $value )  {
        echo "<td align='center' width='200'>" . $value . "</td>";
    }
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

1 question, why do you need => $value ?
You output the $value in the loop, you may not need the $key => part unless you want to use the name of the column in some way.
1

Try :

while($row=pg_fetch_assoc($result)){
    echo "<tr>";
    $maxColumnDisplay = 100;
    foreach($row as $col){
        if(!$maxColumnDisplay--) break;
        echo "<td align='center' width='200'>" . $col . "</td>";
    }
    echo "</tr>";
}

Comments

1

Try this:

function printTable(&$result) //result set
{
    /**  Set up Table and Headers **/
    $fieldinfo = $result->fetch_fields();
    echo '<table class="sqldump">' . "\n";
    echo '<tr class="header">' . "\n";
    foreach ($fieldinfo as $fieldval) {
        echo "<td>".$fieldval->name."</td>\n";
    }
    echo "</tr>\n";

    /** dump rows **/
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        foreach ($row as $column) {
            echo "<td>".$column."</td>\n";
        }
        echo "</tr>";
    }
    echo "</table>\n";

    /**  gc  **/
    $result->free();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.