pdo_pgsql_FetchAssoc1.htm
 
Source: pdo_pgsql_FetchAssoc1.txt
Script :

try {
//$dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
$dbconn= new PDO("pgsql:host=$hostname;dbname=pgsdemo1", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';

/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM emp1 ORDER BY fname";
echo "sql passed <br/>";
/*** fetch into an PDOStatement object ***/
$stmt = $dbconn->query($sql);
echo "PDOStatement object... created <br/>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['fname'].' '.$row['lname'].' '.$row['address'].' '.$row['manager'];
echo("<br/>");
}
/** close while ***/
/*** close the database connection ***/
$dbconn= null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}