Specifications
Bear in mind that there is a limit to the number of MySQL connections that can exist at the
same time. The MySQL parameter max_connections determines what this limit is. The pur-
pose of this parameter and the related Apache parameter MaxClients is to tell the server to
reject new connection requests rather than allowing machine resources to be all used at busy
times or when software has crashed.
You can alter both of these parameters from their default values by editing the config-
uration files. To set MaxClients in Apache, edit the httpd.conf file on your system. To set
max_connections for MySQL, edit the file my.conf.
If you use persistent connections and nearly every page in your site involves database access,
you are likely to have a persistent connection open for each Apache process. This can cause a
problem if you leave these parameters set to their default values. By default, Apache allows
150 connections, but MySQL only allows 100. At busy times, there might not be enough con-
nections to go around. Depending on the capabilities of your hardware, you should adjust these
so that each Web server process can have a connection.
Choosing a Database to Use
You will remember that when we are using MySQL from a command line interface, we need
to tell it which database we plan to use with a command such as
use books;
We also need to do this when connecting from the Web. We perform this from PHP with a call
to the mysql_select_db() function, which we have done in this case as follows:
mysql_select_db(“books”);
The mysql_select_db() function has the following prototype:
int mysql_select_db(string database, [int database_connection] );
It will try to use the database called database. You can also optionally include the database link
you would like to perform this operation on (in this case
$db), but if you don’t specify it, the
last opened link will be used. If you don’t have a link open, the default one will be opened as
if you had called mysql_connect().
Querying the Database
To actually perform the query, we can use the mysql_query() function. Before doing this,
however, it’s a good idea to set up the query you want to run:
$query = “select * from books where “.$searchtype.” like ‘%”.$searchterm.”%’”;
Accessing Your MySQL Database from the Web with PHP
C
HAPTER 10
10
ACCESSING
YOUR MYSQL
DATABASE
235
13 7842 CH10 3/6/01 3:36 PM Page 235