deeejay icon

PostgreSQL - Use PHP to Output Data in JSON Format (2)

deeejay | PRO | 06/14/16 09:18:22 PM UTC | 0 ⭐ | 437 👁️ | Never ⏰ | []
PHP |

510 B

|

None

|

0 👍

/

0 👎

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