scott_pgsql_employee_3php.htm |
<?php // database access parameters // alter this as per your configuration $host = "localhost"; $user = "username"; $pass = "password"; $db = "pgsdemo1"; // open a connection to the database server $dbconn = pg_connect ("host=$host dbname=$db user=$user password=$pass"); if (!$dbconn) { die("Could not open connection to database server"); } echo "connection successful <br/>"; $result = pg_query($dbconn, "SELECT * FROM emp ORDER BY ename"); if ($result) { echo "The query executed successfully.<br />\n"; // show name of the fields $i = pg_num_fields($result); echo" <table border='1'><tr>"; for ($j = 0; $j < $i; $j++) { echo "<td class='td1'>"; $fieldname = pg_field_name($result, $j); echo $fieldname." </td>"; } echo "</tr></table>"; // echo"<br/>"; echo" <table border='1'>"; // for ($row = 0; $row < pg_num_rows($result); $row++) { //embbedding for loop echo "<tr/>"; for($y=0; $y < pg_num_fields($result) ; $y++) { $r2 = pg_fetch_result($result, $row, $y); echo " <td class='td2'>".$r2."</td>"; } // nested for echo "</tr>"; } echo "</table>"; } else { echo "The query failed with the following error:<br />\n"; echo pg_last_error($db_handle); } pg_close($dbconn); ?> |