A database is your best choice for storing data in your web application, and the MySQL database server has always been the most popular choice among PHP developers. It’s supported by almost any hosting company offering PHP, which makes it easy to get started with, and you can even download and install it on your own computer, for testing purposes.
MySQL uses the SQL (Structured Query Language) programming language to work with the data, and PHP interacts with MySQL by simply passing SQL code through a set of MySQL functions to the MySQL server, which then returns a result that PHP can interpret. It can seem a bit scary to have to learn a second language to interact with databases, but fortunately SQL is a fairly simple language, which looks a lot like the English language and we will provide you with some good SQL examples, allowing you to do the most common tasks.
In the following chapters we will work with the MySQL database and make it do various things for us. To do it properly, we need some common test data, which you will need to add to a database for which you have access to. The easiest way to do this is to use one of the many MySQL tools, with the most popular one being phpMyAdmin, which is installed on most servers offering PHP and MySQL. If you don’t have access to phpMyAdmin, you can install it, use one of the many downloadable applications or use the MySQL prompt. Whatever you choose, you should execute the following SQL code against your database. In phpMyAdmin, this is done by clicking the button labelled “SQL”:
CREATETABLE `test_users` ( `id` int(11) NOTNULL auto_increment, `name` varchar(100) NOTNULLdefault'', `country` varchar(100) NOTNULLdefault'', PRIMARYKEY (`id`) ); INSERTINTO `test_users` VALUES (1,'David','USA'); INSERTINTO `test_users` VALUES (2,'Sammy','Canada'); INSERTINTO `test_users` VALUES (3,'Heidi','Germany'); INSERTINTO `test_users` VALUES (4,'Pierre','France'); INSERTINTO `test_users` VALUES (5,'Carlos','Spain');