Hướng dẫn PHP này giúp tạo danh sách HTML bằng cơ sở dữ liệu PostgreSQL. Nó rất đơn giản và dễ dàng để tạo danh sách HTML bằng PHP, giống như các cơ sở dữ liệu khác được sử dụng, ngoại trừ chuỗi kết nối cơ sở dữ liệu PostgreSQL và phương thức Postgres để tìm nạp dữ liệu. Show PHP cung cấp các LIB PostgreSQL để truyền đạt PHP với cơ sở dữ liệu Postgres. Chúng tôi sẽ làm theo các bước sau để tích hợp PostgreSQL với PHP.
Cách bật dữ liệu Postgres trong Php.iniChúng tôi sẽ kích hoạt tiện ích mở rộng Postgres từ tệp uncommented, extension=php_pdo_pgsql.dll, uncommented, extension=php_pgsql.dll, uncommented, extension_dir = "c:/wamp/bin/php/php5.3.5/ext/",
Cách kết nối cơ sở dữ liệu Postgres với PHPChúng tôi sẽ sử dụng phương thức Chúng tôi đã tạo một cơ sở dữ liệu ‘thử nghiệm và tạo nhân viên bằng tập lệnh dưới đây. CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) ); Chúng tôi sẽ tạo tệp CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );0 và thêm mã bên dưới, <?php Class dbObj{ /* Database connection start */ var $servername, $username, $password, $dbname, $port, $conn; __construct() { $this->servername = "localhost"; $this->username = "test"; $this->password = "test123"; $this->dbname = "test"; $this->port = "5432"; } function getConnstring() { $con = pg_connect("host=".$this->servername." port=".$this->port." dbname=".$this->dbname." user=".$this->username." password=".$this->password."") or die("Connection failed: ".pg_last_error()); /* check connection */ if (pg_last_error()) { printf("Connect failed: %s\n", pg_last_error()); exit(); } else { $this->conn = $con; } return $this->conn; } } ?> Chúng tôi sẽ tạo tệp CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );1 và thêm mã dưới đây để truy cập các bản ghi dữ liệu từ bảng PostgreSQL.PostgreSQL table. include("connection.php"); class Employee { protected $conn; protected $data = array(); function __construct() { $db = new dbObj(); $connString = $db->getConnstring(); $this->conn = $connString; } public function getEmployees() { $sql = "SELECT * FROM employee"; $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data"); $data = pg_fetch_all($queryRecords); return $data; } } Bao gồm tệp CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );0 ở đầu tệp, tệp này được sử dụng để truy cập đối tượng kết nối cơ sở dữ liệu Postgres. Chúng tôi đã xác định lớp nhân viên và gán đối tượng kết nối vào một biến lớp. Cuối cùng, đã tạo phương thức CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );3 để truy cập dữ liệu nhân viên từ cơ sở dữ liệu Postgres và trả về dữ liệu. Chúng tôi sẽ sử dụng phương thức này sau này vào tệp CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );4 để đặt kết quả. Chúng tôi sẽ tạo CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );4 và bao gồm các tệp CSS và JS LIBS cần thiết. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> Chúng tôi sẽ nhập tệp CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );1 để truy cập dữ liệu từ Postgres bằng phương thức. <?php include("response.php"); $newObj = new Employee(); $emps = $newObj->getEmployees(); ?> Bây giờ, chúng tôi có tất cả dữ liệu của nhân viên trong biến CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );7 PHP. Chúng tôi sẽ lặp lại trên mảng CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );7 và liên kết dữ liệu với cột Bảng HTML bằng phương pháp CREATE TABLE employee ( id char(5), employee_name varchar(100), employee_salary integer, employee_age integer, CONSTRAINT code_title PRIMARY KEY(id) );9. <table id="employee_grid" class="table" width="100%" cellspacing="0"> <thead> <tr> <th>Name</th> <th>Salary</th> <th>Age</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($emps as $key =>$emp) <tr> <td><?php echo $emp['employee_name'] ?></td> <td><?php echo $emp['employee_salary'] ?></td> <td><?php echo $emp['employee_age'] ?></td> <td> <div class="btn-group" data-toggle="buttons"><a href="#" target="_blank" class="btn btn-warning btn-xs" rel="noopener">Edit</a><a href="#" target="_blank" class="btn btn-danger btn-xs" rel="noopener">Delete</a><a href="#" target="_blank" class="btn btn-primary btn-xs" rel="noopener">View</a></div></td> </tr> <?php endforeach;?> Bạn có thể tải xuống mã nguồn và xem bản demo từ liên kết dưới đây. Conclusion:Chúng tôi đã kích hoạt trình điều khiển cơ sở dữ liệu Postgres vào PHP bằng tệp <?php Class dbObj{ /* Database connection start */ var $servername, $username, $password, $dbname, $port, $conn; __construct() { $this->servername = "localhost"; $this->username = "test"; $this->password = "test123"; $this->dbname = "test"; $this->port = "5432"; } function getConnstring() { $con = pg_connect("host=".$this->servername." port=".$this->port." dbname=".$this->dbname." user=".$this->username." password=".$this->password."") or die("Connection failed: ".pg_last_error()); /* check connection */ if (pg_last_error()) { printf("Connect failed: %s\n", pg_last_error()); exit(); } else { $this->conn = $con; } return $this->conn; } } ?>1, lấy dữ liệu từ cơ sở dữ liệu Postgres sử dụng PHP và hiển thị vào bảng HTML. Làm cách nào để kết nối cơ sở dữ liệu Postgres của tôi với trang web của tôi?Chúng tôi sẽ làm theo các bước dưới đây để kết nối máy chủ cơ sở dữ liệu PostgreSQL bằng công cụ GUI PGADMIN:.. Bước 1: Khởi chạy ứng dụng PGADMIN..... Bước 2: Tạo một máy chủ..... Bước 3: Cung cấp tên máy chủ..... Bước4: Cung cấp máy chủ và mật khẩu..... Bước5: Mở rộng máy chủ..... Bước6: Mở công cụ truy vấn .. Làm cách nào để kết nối với cơ sở dữ liệu Postgres?Để kết nối với cơ sở dữ liệu, bạn cần biết tên của cơ sở dữ liệu đích, tên máy chủ và số cổng của máy chủ và tên người dùng bạn muốn kết nối như thế nào.PSQL có thể được nói về các tham số đó thông qua các tùy chọn dòng lệnh, cụ thể là -d, -h, -p và -u.you need to know the name of your target database, the host name and port number of the server, and what user name you want to connect as. psql can be told about those parameters via command line options, namely -d , -h , -p , and -U respectively.
Postgres có giao diện web không?Dịch vụ cơ sở dữ liệu PostgreSQL có thể được quản lý từ bảng điều khiển hoặc từ giao diện web..
Postgres có sử dụng TCP hoặc HTTP không?PostgreSQL sử dụng giao thức dựa trên thông báo để giao tiếp giữa Frontends và phụ trợ (máy khách và máy chủ).Giao thức được hỗ trợ trên TCP/IP và cũng trên các ổ cắm miền đơn vị.TCP/IP and also over Unix-domain sockets. |