Home > Article > Backend Development > What are the common PostgreSQL database operations in PHP programming?
What are the common PostgreSQL database operations in PHP programming?
PostgreSQL is a free and open source relational database management system with the characteristics of scalability, high security, and high reliability. PHP is a scripting language widely used in Web development. It has the advantages of being easy to learn and high development efficiency. In web development, the application of PHP combined with PostgreSQL can realize a wealth of functions, including data management, user rights management, and data analysis.
In order to better realize the cooperation between PHP and PostgreSQL, we need to understand some common PostgreSQL database operations.
It is very simple to establish a database connection in PHP, just use the following code:
$conn = pg_connect("host=localhost dbname=mydb user=postgres password=mypassword");
Where, host represents the database Address, dbname represents the name of the database to be connected, user and password represent the username and password to log in to the database.
Querying data in the database is a common operation. We can use the pg_query function to perform query operations, and then use the pg_fetch_array function to convert the query results into a PHP array. The following is a sample code that queries employee information in the database:
$result = pg_query($conn, "SELECT * FROM employees"); if (!$result) { echo "An error occurred. "; exit; } while ($row = pg_fetch_array($result)) { echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Age: " . $row['age']; }
The above code executes a query and outputs the query results to the screen.
Inserting data is an operation to modify the database. We can use the pg_query function to perform the insert operation and then check whether it was successful. The following is a sample code that inserts a new employee information into the database:
$fname = "John"; $lname = "Doe"; $age = 25; $result = pg_query($conn, "INSERT INTO employees (first_name, last_name, age) VALUES ('$fname', '$lname', $age)"); if (!$result) { echo "An error occurred. "; exit; } echo "New record created successfully!";
Updating data is also an operation to modify the database. We can use the pg_query function to perform the update operation and then check whether it was successful. The following is a sample code for updating employee information:
$id = 1; $age = 28; $result = pg_query($conn, "UPDATE employees SET age = $age WHERE id = $id"); if (!$result) { echo "An error occurred. "; exit; } echo "Record updated successfully!";
Deleting data is also an operation to modify the database. We can use the pg_query function to perform the delete operation and then check whether it was successful. The following is a sample code to delete employee information:
$id = 1; $result = pg_query($conn, "DELETE FROM employees WHERE id = $id"); if (!$result) { echo "An error occurred. "; exit; } echo "Record deleted successfully!";
The above is a sample code for some common PostgreSQL database operations. Of course, in actual applications, we need to write more complex code according to specific needs. By using these operations flexibly, we can better use PHP and PostgreSQL to implement richer web applications.
The above is the detailed content of What are the common PostgreSQL database operations in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!