<?php

/**
 * PostgreSQL Connection Test Script
 * Tests database connectivity using pg_connect()
 */

// Database connection parameters
$db_config = [
    'host'     => '192.168.50.3',
    'port'     => '5432',
    'dbname'   => 'VelityGlobal',
    'user'     => 'velity',
    'password' => 'Velity2025'
];

// Build connection string
$connection_string = sprintf(
    "host=%s port=%s dbname=%s user=%s password=%s",
    $db_config['host'],
    $db_config['port'],
    $db_config['dbname'],
    $db_config['user'],
    $db_config['password']
);

echo "PostgreSQL Connection Test\n";
echo "==========================\n\n";

// Attempt to connect
echo "Attempting to connect to PostgreSQL...\n";
$connection = @pg_connect($connection_string);

if (!$connection) {
    die("Connection failed: Unable to connect to PostgreSQL database.\n");
    echo "Connection fale!\n";
}else{
}

echo "✓ Successfully connected to PostgreSQL!\n\n";

// Get connection info
$db_version = pg_version($connection);
echo "Database Information:\n";
echo "- PostgreSQL Version: " . $db_version['server'] . "\n";
echo "- Host: " . pg_host($connection) . "\n";
echo "- Port: " . pg_port($connection) . "\n";
echo "- Database: " . pg_dbname($connection) . "\n\n";


echo "\n";

// Close connection
echo "\nClosing connection...\n";
pg_close($connection);
echo "✓ Connection closed successfully.\n";

echo "\n✅ All tests completed!\n";

/**
 * Alternative connection methods you can test:
 * 
 * 1. Using individual parameters:
 * $connection = pg_connect("host=localhost dbname=test_db user=postgres password=your_password");
 * 
 * 2. Using environment variables:
 * putenv("PGHOST=localhost");
 * putenv("PGPORT=5432");
 * putenv("PGDATABASE=test_db");
 * putenv("PGUSER=postgres");
 * putenv("PGPASSWORD=your_password");
 * $connection = pg_connect("");
 * 
 * 3. Using persistent connection:
 * $connection = pg_pconnect($connection_string);
 */
?>