Database Reference
In-Depth Information
PHP API
One of the mostpopular programming language and database engine combinations for the
Web is PHP with MySQL. This combination works well for many reasons, but primarily
the speed, stability, and simplicity that both offer. In addition, PHP scripts can be used eas-
ily with HTML to generate web pages. This section provides a basic tutorial on how to
connect to MySQL and how to query MySQL with PHP using the PHP API, all within a
web page.
Installing and Configuring
There are actually three popularAPIs that may be used to connect to MySQL with PHP.
It's recommended that you use the mysqli ( MySQL Improved ) extension, which replaces
the older mysql extension. We'll use the mysqli API for the programming examples in
this section.
On many Linux systems, PHP is already installed. However, you can use an installation
utility like yum to install PHP, as well as the PHP API, mysqli . You would do that like
this:
yum install php php-mysql
If you'll be executing PHP code within web pages, which is a very nice feature, you may
have to make an adjustment to your web server configuration. If you're using Apache, you
may have to add the AddType directiveto your Apache configuration to tell the web serv-
er to execute code snippets with PHP. You can either put the following line in the web serv-
er's configuration file ( httpd.conf ) to make it global, or add it to a .htaccess file in the dir-
ectory where the HTML pages containing the PHP code snippets are located:
AddType application/x-httpd-php .html
If you add this directive to the httpd.conf configuration file, you'll have to restart the
Apache web service for it to take effect. You won't have to do that with the .htaccess file.
To use PHP with MySQL, you may also have to enable MySQL with PHP by configuring
PHP with the --with-mysql= /path_to_mysql option. That won't be necessary,
though, if you installed the PHP API using yum .
Connecting to MySQL
For PHP code tointerface with MySQL, it must first make a connection to MySQL to es-
tablish a MySQL client session. This bit of code will do that:
Search WWH ::




Custom Search