TOC

The community is working on translating this tutorial into Turkish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

PHP and MySQL:

Introduction to MySQL with PHP

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":

CREATE TABLE `test_users` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL default '',
  `country` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
); 

INSERT INTO `test_users` VALUES (1,'David','USA');
INSERT INTO `test_users` VALUES (2,'Sammy','Canada');
INSERT INTO `test_users` VALUES (3,'Heidi','Germany');
INSERT INTO `test_users` VALUES (4,'Pierre','France');
INSERT INTO `test_users` VALUES (5,'Carlos','Spain');

When your done, a new table called "test_users" should have been created and filled with a small amount of testing data. In the next chapters we will work with it. If you're working with your own installation of PHP, please make sure that MySQL support has been enabled. This can be done in the php.ini file.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!