Hướng dẫn dùng postgresql oid trong PHP
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 This document discusses how to insert data into a table using PostgreSQL INSERT command. We have also covered how to do the same using PHP-PostgreSQL.KJVQCSV69JFX Usage Following is the usage of PostgreSQL INSERT command for inserting data into a single row of a PostgreSQL table. Where table_name is the associated table, column1, 2, 3 are column
names and value 1, 2, 3 are values to be inserted. Insert single row Structure of the table Following is the structure of the table where we will insert data. Command to insert data View data This is from where you can check data using PgAdmin III. This is the data we have entered using INSERT command - Insert multiple rows Here is the command to insert multiple rows in a PostgreSQL database.
Insert data: PHP-PostgreSQL The following example code is to insert data into the 'book' table using PHP. Assume that the name of the file saved with this code is insert.php.
|