- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Introduction to PostgreSQL PHP programming
In the first chapter of the PostgreSQL PHP tutorial, we will provide necessary definitions. We will show, how to install PostgreSQL database and the required packages. The examples will be run on the command line using the PHP CLI.
In order to run the examples, we need to have PHP language (in the form of PHP CLI) and the PostgreSQL database installed. We also need the php5-psql
package.
About PostgreSQL database
PostgreSQL is a powerful, open source object-relational database system. It is a multi-user database management system. It runs on multiple platforms including Linux, FreeBSD, Solaris, Microsoft Windows and Mac OS. PostgreSQL is developed by the PostgreSQL Global Development Group.
Setting up PostgreSQL
We must have PostgreSQL database installed.
$ sudo apt-get install postgresql
On an Debian based system we can install the PostgreSQL database using the above command.
$ sudo update-rc.d -f postgresql remove Removing any system startup links for /etc/init.d/postgresql ... /etc/rc0.d/K21postgresql /etc/rc1.d/K21postgresql /etc/rc2.d/S19postgresql /etc/rc3.d/S19postgresql /etc/rc4.d/S19postgresql /etc/rc5.d/S19postgresql /etc/rc6.d/K21postgresql
If we install the PostgreSQL database from packages, it is automatically added to the start up scripts of the operating system. If we are only learning to work with the database, it is unnecessary to start the database each time we boot the system. The above command removes any system startup links for the PostgreSQL database.
$ /etc/init.d/postgresql status Running clusters: 9.1/main $ service postgresql status Running clusters: 9.1/main
We check if the PostgreSQL server is running. If not, we need to start the server.
$ sudo service postgresql start * Starting PostgreSQL 9.1 database server [ OK ]
On Ubuntu Linux we can start the server with the service postgresql start
command.
$ sudo service postgresql stop [sudo] password for janbodnar: * Stopping PostgreSQL 9.1 database server [ OK ]
We use the service postgresql stop
command to stop the PostgreSQL server.
$ sudo -u postgres createuser janbodnar Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) y Shall the new role be allowed to create more new roles? (y/n) n
We create a new role in the PostgreSQL system. We allow it to have ability to create new databases. A role is a user in a database world. Roles are separate from operating system users. We have created a new user without the -W
option, e.g. we have not specified a password. This enables us to connect to a database with this user without password authentication. Note that this works only on localhost.
$ sudo -u postgres createdb testdb -O janbodnar
The createdb
command creates a new PostgreSQL database with the owner janbodnar.
PHP CLI
PHP language is known to run on the web server. But it can be used on the command line as well. PHP Command Line Interface (PHP CLI) is a library that enables programmers to use PHP on the command line. With PHP CLI we can build GUI applications with PHP-GTK or we can create simple test scripts. In this tutorial, we will connect to the PostgreSQL database using the command line PHP interpreter.
$ sudo apt-get install php5-cli
We install the PHP CLI module on our Linux system.
The php5-pgsql package
The php5-pgsql is a package to work with the PostgreSQL database from the PHP language. On other systems the package name might be different.
$ sudo apt-get install php5-pgsql
We launch the above command to install the package.
Handling errors
We have a quick remark on handling errors in PHP. PHP has a built-in support for error reporting. The specifics can be controlled in the php.ini file. Note that the PHP CLI version has a separate INI file. It is located in /etc/php5/cli/php.ini
on our system.
The display_errors
directive controls, whether the built-in error messages are shown or not. In development environments, these error messages are displayed. In production, they are suppressed. There is no reason to show these technical messages to the user. In addition, it is a potential security risk.
In general, we should log the more specific error messages to a log file. The log_errors
directive controls if the errors are logged or not. The error_log
specifies the name of the file where script errors should be logged. If it is not set, the default is the stderr
for PHP CLI.
The pg_last_error()
functions gets the last error message string of a connection. It is the same error message that is generated in the built-in error reporting.
In the examples of this tutorial, we do not use the pg_last_error()
function, since it duplicates the built-in error messages. We have the following settings:
... display_errors = On ... log_errors = On ; Our own custom based log file error_log = /home/janbodnar/.phpcli_log ...
We display the built-in errors; they are shown on the command line. The error messages are also logged to a specified log file. If we do not want the error messages on the console, we simply turn off the display_errors directive.
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");
In the scripts, we use the die()
function to display a simple error message, which can be easily understood. The more specific details are saved to the log file. The die()
function also terminates the script.
Version
In the first code example, we will get the version of the PostgreSQL database.
<?php $host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb"; $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $query = "SELECT VERSION()"; $rs = pg_query($con, $query) or die("Cannot execute query: $query\n"); $row = pg_fetch_row($rs); echo $row[0] . "\n"; pg_close($con); ?>
In the above PHP script we connect to the previously created testdb
database. We execute an SQL statement which returns the version of the PostgreSQL database.
$host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb";
These are connections string variables. To create a connection to a PostgreSQL database, we must provide the host name, user name and password and the database name.
$con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n");
We connect to the database server. If the connection could not be created the die()
function terminates the script and prints an error message to the console. The pg_connect()
function returns a connection resource, which is going to be used later with other module functions.
$query = "SELECT VERSION()";
This SQL statement selects the version of the PostgreSQL database.
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");
The query is executed with the pg_query()
function.
$row = pg_fetch_row($rs);
We fetch the data from the returned result.
echo $row[0] . "\n";
We print the data that we have retrieved to the console. The data was returned in the form of a PHP array. The first element of the array is the string we are looking for.
pg_close($con);
The connection to the database is closed using the pg_close()
function.
$ php version.php PostgreSQL 9.1.3 on i686-pc-linux-gnu, compiled by gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1, 32-bit
Running the version.php script.
Inserting data
We will create a cars table and insert several rows to it.
<?php $host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb"; $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $query = "DROP TABLE IF EXISTS cars"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "CREATE TABLE cars(id INTEGER PRIMARY KEY, mame VARCHAR(25), price INT)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(1,'Audi',52642)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(2,'Mercedes',57127)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(3,'Skoda',9000)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(4,'Volvo',29000)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(5,'Bentley',350000)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(6,'Citroen',21000)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(7,'Hummer',41400)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(8,'Volkswagen',21606)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); pg_close($con); ?>
The above script creates a Cars
table and inserts 8 rows into the table.
$query = "DROP TABLE IF EXISTS cars"; pg_query($con, $query) or die("Cannot execute query: $query\n");
We drop the cars table if it already exists. The pg_query()
function executes the given query on the specified database connection.
$query = "CREATE TABLE cars(id INTEGER PRIMARY KEY, mame VARCHAR(25), price INT)";
This SQL statement creates a new cars table. The table has three columns.
$query = "INSERT INTO cars VALUES(1,'Audi',52642)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO cars VALUES(2,'Mercedes',57127)"; pg_query($con, $query) or die("Cannot execute query: $query\n");
We are inserting two cars into the table.
pg_close($con);
The connection to the database is closed.
$ psql testdb psql (9.1.3) Type "help" for help. testdb=# SELECT * FROM cars; id | name | price ----+------------+-------- 1 | Audi | 52642 2 | Mercedes | 57127 3 | Skoda | 9000 4 | Volvo | 29000 5 | Bentley | 350000 6 | Citroen | 21000 7 | Hummer | 41400 8 | Volkswagen | 21606 9 | BMW | 36000 (9 rows)
We verify the written data with the psql tool.
Prepared statements
Now we will concern ourselves with prepared statements. When we write prepared statements, we use placeholders instead of directly writing the values into the statements. Prepared statements increase security and performance.
A prepared statement is a server-side object that can be used to optimize performance. When a query is prepared, it is parsed, rewritten, and planned. Later the prepared statement need only be executed. Thus, the parsing, rewriting, and planning stages are only performed once, instead of every time the statement is executed. Prepared statements only last for the duration of the current database session. When the session ends, the prepared statement is forgotten, so it must be recreated before being used again.
$host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb"; $id = 9; $name = "BMW"; $price = 36000; $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $query = "INSERT INTO cars VALUES($1, $2, $3)"; pg_prepare($con, "prepare1", $query) or die ("Cannot prepare statement\n"); pg_execute($con, "prepare1", array($id, $name, $price)) or die ("Cannot execute statement\n"); echo "Row successfully inserted\n"; pg_close($con);
We add a row to the cars table. We use a prepared query.
$id = 9; $name = "BMW"; $price = 36000;
We have three variables that will be used to build a query. These values could come e.g. from a web form.
$query = "INSERT INTO cars VALUES($1, $2, $3)";
This is an SQL query with $1
, $2
, and $3
placeholders. The placeholders will be filled later.
pg_prepare($con, "prepare1", $query) or die ("Cannot prepare statement\n");
Here we prepare a query by calling the pg_prepare()
function. The second parameter of the function is the name of the prepared statement. It must be unique per-connection. Prepared statements are faster and guard against SQL injection attacks.
pg_execute($con, "prepare1", array($id, $name, $price)) or die ("Cannot execute statement\n");
The pg_execute()
function sends a request to execute a prepared statement with given parameters, and waits for the result. The values are bound to the placeholders.
$ php prepared.php Row successfully inserted testdb=# SELECT * FROM cars; id | name | price ----+------------+-------- 1 | Audi | 52642 2 | Mercedes | 57127 3 | Skoda | 9000 4 | Volvo | 29000 5 | Bentley | 350000 6 | Citroen | 21000 7 | Hummer | 41400 8 | Volkswagen | 21606 9 | BMW | 36000 (9 rows)
We have inserted a new car into the table.
Sources
The PostgreSQL PHP manual and the PostgreSQL documentation were consulted to create this tutorial.
This was an introductory chapter to PostgreSQL PHP tutorial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论