PostgreSQL - Use PHP to Output Data in JSON Format (2)
Create pg.query.inc.php
===
<?php
$connectString = 'host=db_host.org port=5432 dbname=db_name user=your_username password=your_password';
$link = pg_connect($connectString);
if (!$link) {
echo "error";
} else {
$result = pg_query($link, $sql);
$rows = array();
while($r = pg_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
Create php file and pass in db connection
===
<?php
$sql = "SELECT id, first_name, last_name FROM person";
include 'pg.query.inc.php';
?>
Comments