In order to create tables, add records, and request information from a database, some sort of client is necessary to communicate with the MySQL server. Later in the book, PHP scripts will act in this role, but being able to use another interface is necessary. Although there are oodles of client applications available, I'll focus on two: the mysql client and the Web-based phpMyAdmin. A third option, the MySQL Query Browser, is not discussed in this book but can be found at the MySQL Web site (www.mysql.com), should you not be satisfied with these two choices.
The rest of this chapter assumes you have access to a running MySQL server. If you are working on your own computer, see Appendix A, "Installation," for instructions on installing MySQL, starting MySQL, and creating MySQL users (all of which must already be done in order to finish this chapter). If you are using a hosted server, your Web host should provide you with the database access. Depending upon the hosting, you may be provided with phpMyAdmin, but not be able to use the command-line mysql client.
Using the mysql Client
The mysql client is normally installed with the rest of the MySQL software. Although the mysql client does not have a pretty graphical interface, it's a reliable, standard tool that's easy to use and behaves consistently on many different operating systems.
The mysql client is accessed from a command-line interface, be it the Terminal application in Linux or Mac OS X , or a DOS prompt in Windows . If you're not comfortable with command-line interactions, you might find this interface to be challenging, but it becomes easy to use in no time.
To start the application from the command line, type its name and press Return or Enter:
Depending upon the server (or your computer), you may need to enter the full path in order to start the application. For example:
/Applications/MAMP/Library/bin/mysql (Mac OS X, using MAMP)
C:\xampp\mysql\bin\mysql (Windows, using XAMPP)
When invoking this application, you can add arguments to affect how it runs. The most common arguments are the username, password, and hostname (computer name, URL, or IP address) you want to connect using. You establish these arguments like so:
mysql -u username -h hostname –p
The -p option will cause the client to prompt you for the password. You can also specify the password on this line if you prefer—by typing it directly after the -p prompt—but it will be visible, which is insecure. The -hhostname argument is optional, and you can leave it off unless you cannot connect to the MySQL server without it.
Within the mysql client, every statement (SQL command) needs to be terminated by a semicolon. These semicolons are an indication to the client that the query is complete and should be run. The semicolons are not part of the SQL itself (this is a common point of confusion). What this also means is that you can continue the same SQL statement over several lines within the mysql client, which makes it easier to read and to edit, should that be necessary.
As a quick demonstration of accessing and using the mysql client, these next steps will show you how to start the mysql client, select a database to use, and quit the client. Before following these steps,
- The MySQL server must be running.
- You must have a username and password with proper access.
Both of these ideas are explained in Appendix A.
As a side note, in the following steps and throughout the rest of the book, I will continue to provide images using the mysql client on both Windows and Mac OS X. While the appearance differs, the steps and results will be identical. So in short, don't be concerned about why one image shows the DOS prompt and the next a Terminal.
To use the mysql client
- Access your system from a command-line interface.
On Unix systems and Mac OS X, this is just a matter of bringing up the Terminal or a similar application.
If you are using Windows and installed MySQL on your computer, choose Run from the Start menu (or press Windows Key+R), type cmd in the window , and press Enter (or click OK) to bring up a DOS prompt.
- Invoke the mysql client, using the appropriate command.
/path/to/mysql/bin/mysql -u username -p
The /path/to/mysql part of this step will be largely dictated by the operating system you are running and where MySQL was installed. I've already provided two options, based upon installations of MAMP on Mac OS X or XAMPP on Windows (both are installed in Appendix A).
The basic premise is that you are running the mysql client, connecting as username, and requesting to be prompted for the password. Not to overstate the point, but the username and password values that you use must already be established in MySQL as a valid user (see Appendix A).
- Enter the password at the prompt and press Return/Enter.
The password you use here should be for the user you specified in the preceding step. Ife you used the proper username/password combination (i.e., someone with valid access), you should be greeted as shown in. If access is denied, you're probably not using the correct values (see Appendix A for instructions on creating users).
If you are successfully able to log in, you'll see a welcome message like this.
- Select the database you want to use.
The USE command selects the database to be used for every subsequent command. The test database is one that MySQL installs by default. Assuming it exists on your server, all users should be able to access it.
- Quit out of mysql.
You can also use the command quit to leave the client. This step—unlike most other commands you enter in the mysql client—does not require a semicolon at the end.
- Quit the Terminal or DOS console session.
The command exit will terminate the current session. On Windows, it will also close the DOS prompt window.
phpMyAdmin (www.phpmyadmin.net) is one of the best and most popular applications written in PHP. Its sole purpose is to provide an interface to a MySQL server. It's somewhat easier and more natural to use than the mysql client but requires a PHP installation and must be accessed through a Web browser. If you're running MySQL on your own computer, you might find that using the mysql client makes more sense, as installing and configuring phpMyAdmin constitutes unnecessary extra work (although all-in-one PHP and MySQL installers may do this for you). If using a hosted server, your Web host is virtually guaranteed to provide phpMyAdmin as the primary way to work with MySQL and the mysql client may not be an option.
Using phpMyAdmin isn't hard, but the next steps run through the basics so that you'll know what to do in the following chapters.
To use phpMyAdmin
- Access phpMyAdmin through your Web browser.
The URL you use will depend upon your situation. If running on your own computer, this might be http://localhost/phpMyAdmin/. If running on a hosted site, your Web host will provide you with the proper URL. In all likelihood, phpMyAdmin would be available through the site's control panel (should one exist).
Note that phpMyAdmin will only work if it's been properly configured to connect to MySQL with a valid username/password/hostname combination. If you see a message like the one in , you're probably not using the correct values (see Appendix A for instructions on creating users).
- If possible and necessary, use the list on the left to select a database to use.
Use the list of databases on the left side of the window to choose with which database you want to work. This is the equivalent of running a USE databasename query within the query within the mysql client.
What options you have here will vary depending upon what MySQL user phpMyAdmin is connecting as. That user might have access to one database, several databases, or every database. On a hosted site where you have just one database, that database will probably already be selected for you. On your own computer, with phpMyAdmin connecting as the MySQL root user, you would see a pull-down menu or a simple list of available databases.
- Click on a table name in the left column to select that table .
You don't always have to select a table—in fact you never will if you just use the SQL commands in this book, but doing so can often simplify some tasks.
- Use the tabs and links (on the right side of the page) to perform common tasks.
For the most part, the tabs and links are shortcuts to common SQL commands. For example, the Browse tab performs a SELECT query and the Insert tab creates a form for adding new records.
- Use the SQL tab or the SQL query window to enter SQL commands.
The next three chapters, and a couple more later in the book, will provide SQL commands that must be run to create, populate, and manipulate tables. These might look like
INSERT INTO tablename (col1, col2) VALUES (x, y)
These commands can be run using the mysql client, phpMyAdmin, or any other interface. To run them within phpMyAdmin, just enter them into one of the SQL prompts and click Go.