1

I have never programmed anything in php before and haven't touched html in 10 years. I could use some help. I am querying a postgresql database using php. I am trying to display my query results in a table format with headers like this:

first_name   last_name   employee_id
tom          jones       111
bob          barker      112
bill         davis       113

Sample code I am trying to get to work correctly:

echo("<table border=2");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    foreach ($line as $col_value => $row_value) {
        echo("<tr><td>$col_value</td><td>$row_value</td></tr>\n");
    }
}
echo("</table>");

My formatting is being displayed like this:

first_name tom
last_name jones
employee_id 111
first_name bob
last_name barker
employee_id 112
first_name bill
last_name davis
employee_id 113

As you can see I am storing my query in an associative array.

Thanks for any help.

2
  • You might want to look into Symfony, if you've a programming background but are unfamiliar with PHP. Symfony2 combined with Doctrine2 is an excellent PHP framework. Commented Jul 1, 2011 at 7:45
  • Looking at it again, my answer fixes an html formatting issue that was there but the data would still be displayed like your example. I saw the missing > and thought that was it...my mistake. Dirk's answer looks like it will get you what except for the column headers which you would have to deal with manually. Commented Jul 1, 2011 at 10:07

4 Answers 4

3

Looks like you might be missing a bracket on the opening table tag:

Try changing this:

echo("<table border=2");

to this:

echo('<table border="2">');

and see if that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I wish it were that easy ; ).
1
echo("<table border=2><tr><td>first_name</td><td>last_name</td><td>employee_id</td></tr>");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    echo("<tr>");
    foreach ($line as $col_value => $row_value) {
        echo("<td>$row_value</td>");
    }
    echo("</tr>\n");
}
echo("</table>");

Or:

echo("<table border=2><tr><td>first_name</td><td>last_name</td><td>employee_id</td></tr>");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    echo("<tr><td>".$line[0]."</td><td>".$line[1]."</td><td>".$line[2]."</td></tr>\n");
}
echo("</table>");

2 Comments

Thanks a bunch that is very close to what I am looking for. Do you have any ideas on how I could dynamically create the table column headers rather than hard coding them in?
SELECT COLUMN_NAME FROM information_schema.`COLUMNS` C WHERE table_name = '<your table name>' ?
0
echo "<table>\n";
echo("<table border=2");
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value => $row_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
    break;
}
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

Comments

-1
<?php
echo "<table width=100% border="1">";
while ($row = mysql_fetch_array($result))
 {  
  $id= $row["id"]; 
  $f_name= $row["f_name"]; 
  echo "<tr><td>";
  echo $id;
  echo"</td>";
  echo"<td>";
  echo $f_name;
  echo"</tr>";
 } 
echo"</table>";
?>

try this may help

1 Comment

I know this is 13 year old answer but question explicitly said "querying a postgresql database" and you answer with mysql_fetch..

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.