| | |
Accessing PostgreSQL Database from PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
This code snippet will attempt to show newbies to php how to connect and retrieve data from a PostgreSQL Database Server using PHP.
<?php // Initiate the connection $conn = pg_connect('dbname=<database>'); /* <database> is the name of your database. It is assumed to reside on the server you are running this code from Alternatively use something like this: $conn = pg_connect('host=myserver port=5432 dbname=database user=someuser password=secret'); */ if (!$conn) { // Check if valid connection // NO, some error occured echo "Error Connecting to database<br>"; exit; // add your error handling function here or just exit :) } else { // Valid connection, we can go on to retrieve some data /* Assume we have a table with user data called user_table Username Password Userlevel user1 pass1 1 user2 pass2 2 etc. 1.) Retrieve all rows and display it in a table */ $query = "select * from user_table order by Username"; $result = pg_query($conn,$query); if (!$result) { // Error on Query // Your Error code Here } else { numrows = pg_numrows($result); echo "<table>"; for ($i=0; $i < $numrows ; $i++) { $DataArr = pg_fetch_rows($result,$i); echo "<tr><td>$DataArr[0]</td><td>$DataArr[1</td><td>$DataArr [2]<td></tr>"; } // For echo "</table>"; } // Query } // Connection pg_close($conn); ?>
Similar Threads
- PHP-PostgreSQL (PHP)
- psycopg2: database connectivity to PostgreSQL (Python)
- Postgresql Distributed Database (Database Design)
- exporting Excel tables into PostgreSQL database (Python)
- Access PostgreSQL from PHP (PHP)
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error errors execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javascript joomla keywords limit link login loop mail menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube



