User Tools

Site Tools


classes:general:dbsamples:php_postgres

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
classes:general:dbsamples:php_postgres [2017/01/14 19:40] – created localadminclasses:general:dbsamples:php_postgres [2021/08/11 17:15] (current) – [Command line connection example for Postgres database connection] localadmin
Line 8: Line 8:
  
 <?php <?php
- #make a connection to the database + // Make a connection to the database 
-        #the values here MUST BE CHANGED to match the database and credentials you wish to use+        // The values here MUST BE CHANGED to match the database and credentials you wish to use
  $dbhost = pg_connect("host=hostname dbname=databasename user=username password=password");  $dbhost = pg_connect("host=hostname dbname=databasename user=username password=password");
  
- If the $dbhost variable is not defined, there was an error+ // If the $dbhost variable is not defined, there was an error
  if(!$dbhost)  if(!$dbhost)
  {  {
Line 18: Line 18:
  }  }
  
- Define the SQL query to run (replace these values as well)+ // Define the SQL query to run (replace these values as well)
  $sql = "SELECT * FROM Schema.Tablename";  $sql = "SELECT * FROM Schema.Tablename";
  
-        #run the SQL query+        // Run the SQL query
  $result = pg_query($dbhost, $sql);  $result = pg_query($dbhost, $sql);
  
-        If the $result variable is not defined, there was an error in the query+        // If the $result variable is not defined, there was an error in the query
  if (!$result)  if (!$result)
  {  {
Line 30: Line 30:
  }  }
  
- Iterate through each row of the result + // Iterate through each row of the result 
  while ($row = pg_fetch_array($result))  while ($row = pg_fetch_array($result))
  {  {
- #write HTML to the page, replace this with whatever you wish to do with the data+ // Write HTML to the page, replace this with whatever you wish to do with the data
  echo $row[0]."<br/>\n";  echo $row[0]."<br/>\n";
  }  }
  
- + // Free the result from memory
-Free the result from memory+
  pg_free_result($result);  pg_free_result($result);
  
- Close the database connection+ // Close the database connection
  pg_close($dbhost);  pg_close($dbhost);
 ?> ?>
Line 50: Line 49:
  
 </code> </code>
 +
 +
classes/general/dbsamples/php_postgres.1484444401.txt.gz · Last modified: 2017/01/14 19:40 by localadmin