Own Design
Own Design
Your Designer

Lesson 17: Databases

Lesson 17: Databases

A database is a collection of information / data that is organized so that it can easily be retrieved, administrated and updated. Databases thereby enable the opportunity to create dynamic websites with large amounts of information. For example, all data on members of HTML.net and all posts in the forums are stored in databases.

A database usually consists of one or more tables. If you are used to working with spreadsheets, or maybe have used databases before, tables will look familiar to you with columns and rows:

Table

There are many different databases: MySQL, MS Access, MS SQL Server, Oracle SQL Server and many others. In this tutorial, we will use the MySQL database. MySQL is the natural place to start when you want to use databases in PHP.

You need to have access to MySQL in order to go through this lesson and the next lessons:

  • If you have a hosted website with PHP, MySQL is probably already installed on the server. Read more at your web host's support pages.
  • If you have installed PHP on your computer yourself and have the courage to install MySQL as well, it can be downloaded in a free version (MySQL Community Edition) at the MySQL's website.
  • If you use XAMPP (see lesson 2), MySQL is already installed and ready to use on your computer. Just make sure MySQL is running in the Control Panel:

    XAMPP

In the rest of this lesson, we will look more closely at how you connect to your database server, before we learn to create databases and retrieve and update data in the following sessions.

Connection to database server

First, you need to have access to the server where your MySQL database is located. This is done with the function documentationmysql_connect with the following syntax:

	mysql_connect(server, username, password) 
	

Pretty straightforward: First, you write the location of the database (server), and then type in the username and password.

If you have your own website, you should read about location of your MySQL server on your host's support pages. Username and password will often be the same as those you use for FTP access. Otherwise contact your provider.

Example of a MySQL connection on a hosted website:

	mysql_connect("mysql.myhost.com", "user001", "sesame") or die(mysql_error()); 
	

Example of a MySQL connection with XAMPP (default settings):

	mysql_connect("localhost", "root", "") or die (mysql_error());
	

In the examples are added or die(mysql_error()) which, in brief, interrupts the script and writes the error if the connection fails.

Now we have made a connection to a MySQL server, and can now start creating databases and retrieve and insert data. This is exactly what we will look at in the next lessons.

By the way, keep in mind that it is good practice to close the database connection again when you're finished retrieving or updating data. This is done with the function documentationmysql_close.

owndesign.page.tl
Send Message
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free