Hướng dẫn php check postgresql connection
Việc kết nối tới PostgreSQL Database để lấy dữ liệu là rất dễ dàng. Với PHP có 2 cách để kết nối như sau: Show Nội dung chính
1. Sử dụng hàm pg_connect() của PHPKích hoạt Extension pgsql của PHPĐể sử dụng hàm được hàm pg_connect() của PHP chúng ta cần kích hoạt 1 extension của PHP là: pgsql Sửa file php.ini và mở comment của dòng sau: – Trên Windows: extension=php_pgsql.dll – Trên Linux: extension=pgsql.so Sau đó restart lại php-fpm hoặc apache để cập nhật config. Kiểm tra php đã có kích hoạt module pgsql chưa? Chúng ta sử dụng command sau: php -m | grep pgsql Nếu kết quả như sau tức là module pgsql của PHP đã hoạt động. Kết nối tới PostgreSQL Database sử dụng hàm pg_connect()Tham khảo đoạn code sau: port= Với:
2. Sử dụng class PDO của PHPHãy chắc chắn rằng PDO module của PHP đã được kích hoạt. Nếu không hãy sửa fle php.ini và bỏ comment dòng sau: – Trên Windows: extension=php_pdo_pgsql.dll – Trên Linux là mặc định được kích hoạt Kết nối tới PostgreSQL Database sử dụng hàm PDOTham khảo đoạn code sau: \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_EMULATE_PREPARES => false, ]; // Query string $dsn = "pgsql:host= Nguồn: vinasupport.com (PHP 4, PHP 5, PHP 7, PHP 8) pg_connect — Open a PostgreSQL connection Descriptionpg_connect(string If a second call is made to pg_connect() with the same The old syntax with multiple parameters $conn = pg_connect("host", "port", "options", "tty", "dbname") has been deprecated. Parametersconnection_string The The currently recognized parameter keywords are: The flags If If Return Values Returns an PgSql\Connection instance on success, or ExamplesExample #1 Using pg_connect()
See Also
tim at buttersideup dot com ¶ 14 years ago
lukasz dot wolczak at gmail dot com ¶ 6 years ago
matias at nospam dot projectcast dot com ¶ 20 years ago
Anonymous ¶ 7 years ago
thakur at corexprts dot com ¶ 11 years ago
Dave ¶ 9 years ago
Anonymous ¶ 17 years ago
VLroyrenn ¶ 3 years ago
$before_conn2 - $before_conn1); // Takes ~0.03s gutostraube at gmail dot com ¶ 12 years ago
bgalloway at citycarshare dot org ¶ 14 years ago
phpnet at benjamin dot schulz dot name ¶ 18 years ago
kayotix at yahoo dot com ¶ 21 years ago
leace at post dot cz ¶ 22 years ago
floriparob at gmail dot com ¶ 6 years ago
xzilla at users dot sourceforge dot net ¶ 18 years ago
Cybertinus ¶ 18 years ago
Anonymous ¶ 7 years ago
derry at siliconriver.com dot au ¶ 19 years ago
Sohel Taslim ¶ 15 years ago
rolf at sir-wum dot de ¶ 20 years ago
borovik -at- gmail ¶ 15 years ago
xourge ¶ 15 years ago
dreamsoundaudio at gmail dot com ¶ 11 years ago
Anonymous ¶ 4 years ago
$klient; infotirona at yahoo dot com ¶ 12 years ago
|