classes:general:dbsamples:php_postgres
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
classes:general:dbsamples:php_postgres [2017/03/21 06:26] – localadmin | classes: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 |
- | | + | |
$dbhost = pg_connect(" | $dbhost = pg_connect(" | ||
- | # 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 = " | $sql = " | ||
- | | + | |
$result = pg_query($dbhost, | $result = pg_query($dbhost, | ||
- | | + | |
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]."< | echo $row[0]."< | ||
} | } | ||
- | + | // 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: | ||
</ | </ | ||
+ | |||
+ |
classes/general/dbsamples/php_postgres.1490095594.txt.gz · Last modified: 2017/03/21 06:26 by localadmin