happy, and then management decided to change database products. They heard about this absolutely
incredible database called MySQL and opted to use it instead. Now the question is—how much code
are you going to have to change? The answer—hardly any. A few changes might need to be made to
accommodate discrepancies between database products, but that's about all. Before database-
independent interfaces, the programmer had to code the functions that interfaced with the databases
themselves. Changing a database product meant writing an entirelu new interface from scratch. Not a
lot of fun.
There are many interfaces/drivers available for MySQL. Perl, PHP, C++, Python, and ODBC are just a
few of the programmable interfaces that have been written for MySQL. Of course, you do not have to
use them if you do not want to. Sometimes, it makes more sense for you to do it the old way—creating
an interface from scratch. If that is the case, MySQL includes the C++ source files. You can create your
own interface or improve on the code that is made available to you.
Interfaces are a fast, clean way for a developer to build applications that communicate with databases.
You might be asking yourself, "How do they communicate with the database?" You can think of an
interface as a customer and the driver as a switchboard operator. A switchboard operator takes calls
and routes them to the right person. The driver and the interface share the same relationship. When a
customer makes a call to the switchboard and asks for a specific person, the operator knows the
number that person can be reached at and connects the customer to that number. The interface and the
driver do the same thing. The application tells the interface to connect to the database. The application
provides some information, such as the address and the name of the database to which the application
wants to talk. The interface take this information and makes a call to the driver. The driver receives this
information and makes the connection. It knows which port to talk on and how to talk to the database.
The port is very much like the telephone number for the person you're trying to call. It is a line of
communication. Now data can flow freely between the application and the database.
Connection Requirements
Now that you have a basic understanding of how interfaces and drivers work, you'll move on to the actual
connection. You have already read about the events that lead up to a connection, but you haven't learned
about the requirements to make a connection.
The first requirement for making a connection to a database server, other than some sort of physical
connection, is the driver. The proper driver must be installed. If the driver is not present, there is no way
for your application to communicate with the database. Make sure you are using the most current driver.
A lot of manufacturers update their drivers periodically, especially when a new release for the database
has been made available.
The next requirement is the name or address of the server where the database is located. How can you
make a telephone call if you don't know the number, or pay a friend a visit if you don't know where he or
she lives? A database connection is the same. How can a driver connect to a database if it doesn't
know where to connect? Before you can connect to a database, you need to get the IP address of the
database server or, if you are using dynamic addressing, the server name. This is vital information for
your connection.
Another requirement for making a connection is the database name. This is another important piece of
information the driver will need to access the database. As in the previous example, if you don't know
where to connect, how can you connect? If you don't specify a database, the driver doesn't know where
to get the data.
The final requirement that all database connections need is the username and password for the
connection. It is all about security. The database server will not allow just anybody to connect to it. It
looks at its grant tables to see who is allowed access to which databases. If the username and
password do not match up, a connection will not be made.
Note You can learn more about security issues in Day 17, "MySQL Database Security."
These four requirements are the same for almost every database interface/driver. If you are a developer
getting ready before you can go any further.