To create tables, add records, and request information from a database, you need some sort of client 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 oodles of client applications are available, I’ll focus on two: the mysql client and the web-based phpMyAdmin. A third option, the MySQL Workbench, is not discussed in this book but can be found at the MySQL web site (https://dev.mysql.com/downloads/workbench/), 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 on 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 macOS , 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 on the server (or your computer), you may need to enter the full path to start the application. For example:
mysql (macOS, 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 use to connect. 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 -h hostname 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, a common point of confusion, are not part of the SQL itself. What this also means is that you can continue the same SQL statement over several lines within the mysql client, which makes it easy 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 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 macOS. Although 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 macOS, this is just a matter of bringing up the Terminal or a similar application.
If you are using Windows and you have installed MySQL on your computer, 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 pathtomysql 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 on installations of MAMP on macOS 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 valid (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. If 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).
Select the database you want to use .
After getting into the mysql client, run a USE command to choose the database with which you want to work.
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 is 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, because installing and configuring phpMyAdmin constitutes unnecessary extra work (although all-in-one PHP and MySQL installers may do this for you). If you are 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 on your situation. If running web sites 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. Likely, 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 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 mysql client.
What options you have here will vary depending on 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, you can use options on the Browse tab to perform a SELECT query and options on the Insert tab to add new records.
Use the SQL tab 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 the SQL tab and click Go.