Getting Ready


Before we begin to use databases within our PHP scripts, we need to make sure that PHP is set up to do this. We will show the steps for MySQL here and refer you to the PHP Manual for setting up appropriate extensions for other database servers. The sequence of commands is similar, however.

As we have seen before, PHP ships with a number of extensions that can be optionally compiled or enabled when running the language engine. The mbstring extension we saw in Chapter 6, "Strings and Characters of the World," is a perfect example. To keep PHP reasonably sized and to make compiling it manageable, not all of these extensions are enabled by default. We enable the mbstring extension by adding it to the configuration and compilation for Unix-like systems, or enabling the extension.dll for Microsoft Windows.

To use MySQL within PHP, we will follow a similar process, which is complicated by the fact that two extensions for MySQL are included in PHP5. The first is the traditional mysql extension, which is a collection of useful functions for working with MySQL databases. The second is the Improved MySQL Extension or mysqli, which allows users to take advantage of much of the functionality in newer versions of MySQL. In addition to providing both object-oriented and procedural interfaces, it uses a newer binary protocol to connect to the server. We will use this newer version and object-oriented interface throughout this book.

To enable the mysqli extension in PHP on Unix systems, you need to have a version of MySQL greater than 4.1.2 installed and need to know where the mysql_config program is (typically in the bin/subdirectory of the MySQL installation). You should then add the following option to the configure script when preparing PHP5 for compilation:

 --with-mysqli=/path/to/mysql/installation/bin/mysql_config 

Windows users first need to make sure that they have downloaded and unpacked the various extensions for PHP5 for Windows (see Appendix A, "Installation/Configuration"). After this is done, you need to edit the php.ini file (typically found in C:\windows or the PHP installation directory). Make sure that the following entry exists and is uncommented by removing any semicolons (;) at the beginning:

 extension=php_mysqli.dll  

You can test and see whether the extension is working by running the following script, which uses a handy PHP function called function_exists to see if a function by the given name is callable within code.

 <?php   $si = function_exists('mysqli_connect');   if ($si)   {     echo 'MySQLi appears to be installed correctly<br/>\n';   }   else   {     echo <<<EOM        Whooops! MySQLi does not appear to be compiled or enabled        properly!<br/> EOM;   } ?> 

Make sure that the number of connections that your web server can handle is the same as or less than the number of connections that your database server can handle. For example, MySQL often limits the number of concurrent connections to 100 by default in the my.ini file. Web servers such as Apache's HTTP Server allow up to 150 simultaneous connections by default (controlled in the httpd.conf), and most versions of Microsoft's IIS default to allowing thousands of connections! Both of these rapidly exceed the 100 connections that your MySQL server can support.

You will have to increase the number of connections that MySQL can support by changing the max_connections value in my.ini, decrease the number of simultaneous connections that your web server can support, or add logic to your web applications to handle the inability to get a connection.




Core Web Application Development With PHP And MYSQL
Core Web Application Development with PHP and MySQL
ISBN: 0131867164
EAN: 2147483647
Year: 2005
Pages: 255

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net