0

I am stuck with displaying result into table format. I tried with all options like "pg_fetch_row, pg_fetch_array, pg_fetch_all_columns", but I am not getting results in table format.

The query part is working fine, also I am getting results in XML format, but I need to display it in table format, also it should contain a column heading.

Please can anyone suggest me how to get the results in table format.

My PHP_PostgreSQL code shown in below:

<?
$conn = pg_connect("user=postgres password=nkr@123 host=localhost dbname=test");
    if (!$conn) {
    echo "An error occured.\n";
    exit;
    }

$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
echo "Value of \$cat = $cat <br>Value of \$subcat = $subcat \n";

$result = pg_query($conn, "SELECT * FROM $cat JOIN $subcat ON $cat.districtco=$subcat.districtcode");
if (!$result) {
echo "An error occured in result.\n";
exit;
}
echo $result;
if (!$result) {
echo "An error occured in xml_result.\n";
exit;
}
?>

I am using this code to get results in XML:

$xml_result="<resultxml>";
$num = pg_num_fields($result);
while($row = pg_fetch_array($result)){
$xml_result.="<record>"; 
for ($i=0; $i < $num; $i++)
{   
  $xml_result .= "<".pg_field_name($result, $i).">".$row[$i]."</".pg_field_name($result, $i).">";      
} 
$xml_result.="</record>";
}
$xml_result.="</resultxml>";
echo $xml_result;

1 Answer 1

1

You do know you should process the result set with a for() loop, don't you?

Although psql will spit the result set from a query optionally in HTML, this only happens because psql was specifically written to do this.

2
  • Aside from your own code above, in which you can replace your XML specific tags for the HTML table tags, you can have a look at this page, where you'll find plenty of information. Commented Aug 10, 2012 at 12:25
  • I already made a search in that, and also explored some examples in that, they are working fine but I am not getting results for my data. In my case, the results contains geometry columns to display. Commented Aug 10, 2012 at 12:54

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.