php psql test


<?
class postgress
{

    private $conn = null;

    function __construct($host, $port, $name, $user, $pass)
    {
        $dbconn = pg_connect("host=$host port=$port dbname=$name user=$user password=$pass options='--client_encoding=windows-1251'");
        $this->conn = $dbconn;
        pg_query("set client_encoding = 'windows-1251'");
    }

    function getConnection()
    {
        return $this->conn;
    }

    function __destruct()
    {
        pg_close($this->conn);
    }


    function queryAll($sql){
        try {
            $result = pg_query($this->conn , $sql);
            $res = pg_fetch_all($result) ;
            if ($res)
                return $res;
            else
                return "---> queryAll error :\n" .  print_r( pg_last_error($this->conn),true);

        } catch ( Exception $e){
            return "Exception error :\n" . print_r($e,true);
        }
    }

}

$db = new postgress("194.85.124.73", "5432" , "volgrad" , "postgres", "");

$sql = "select *  from  objem_nalog_lgot ; " ;


$DB_PG = $db;
$res = $DB_PG->queryAll($sql);


//file_put_contents(   "/log/1.txt", $sql. "\n\n\n".print_r($res,true) );

?>
    <style>
        table {
            border-collapse: collapse;
        }
        td {
            border: 1px solid black;
            padding: 4px;
        }
    </style>
    <table  >
        <tr style="background: #acc;">
<?foreach ($res[0] as $key => $value) {
    echo "<td>$key</td>";
}
echo "</tr>";

foreach ($res as $key => $value) {
    echo "<tr>";
    foreach ($value as $key2 => $value2) {
        echo "<td>". $value2 ."</td>";
    }
    echo "</tr>";
}


echo "</table>";