postgres connect to database php code example

Example 1: php connect to postgresql

<?php
    $db_connection = pg_connect("host=localhost dbname=dbname user=username password=password");
	$result = pg_query($db_connection, "SELECT lastname FROM employees");
?>
<?php  // with PDO
    $myPDO = new PDO('pgsql:host=localhost;dbname=dbname', 'username', 'password');
	$result = $myPDO->query("SELECT lastname FROM employees");
?>

Example 2: how to connect to a database in php

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Tags:

Php Example