To get started with MySQL, you’re going to need a platform on which to develop and test your SQL queries. There are two main options here. You can either try running MySQL on your own computer, or (and this is probably the simpler option) use a MySQL hosting account provided to you by a website hosting provider. For as little as $5-$10 per month, you can buy web-space on a MySQL capable server where you can test your code and, eventually, host your finished website.
This is probably the easiest option for a beginner web developer. All it requires is that you purchase an account with a reputable website hosting provider who offer PHP and MySQL accounts (I’d recommend http://www.clook.net because they’re nice people) and once you’ve signed up, you’ll be given the connection details you need.
You can then upload PHP files to your webhost’s public directory, and run commands within the PHP code to access your MySQL database. More information on these commands will be outlined in future tutorials, and to find out how to connect to MySQL from within PHP, skip past the next step of this tutorial
This is an option for slightly more advanced developers. MySQL can be installed as a stand-alone application, but to get the most out of it, and to make the most use of these tutorials, you’ll also need the Apache web server and PHP installed
Here are the files you’ll need if you wish to try your hand at installing Apache and PHP. The exact details of how to achieve this are outwith the scope of this tutorial though, but a quick search on Google for the relevant topics should bring you the help you need
- MySQL (the important one) - http://dev.mysql.com/downloads/mysql/5.0.html#downloads
- PHP - http://www.php.net/downloads.php
- Apache HTTP Server - http://httpd.apache.org/download.cgi
Note : PHP and Apache aren’t explicitly required to use MySQL with a website, but for the purpose of these tutorials, that’s what we’re going to use, since they’re the most widely used combination of technologies for website creation these days. Other alternatives might be Perl, C++, Java etc. and PostgreSQL as a MySQL alternative.
Connecting to MySQL via PHP is a relatively simple process. All you will need is:
- Your MySQL username and password, as provided by the hosting company, or as set up by yourself on your local machine (on my local machine, the username/password is simply root/pass, on a remote website host’s server it’ll probably be something more complex, like my_accountname/f6AhAdxCV)
- A database set up on the remote host that you can connect to - Don’t worry if you’ve not got one set up yet, or don’t know how to set one up if your website host doesn’t provide access - skip ahead to the next tutorial and I’ll explain in more detail how you can do this. With most website hosts, there’s an option in the control panel to create a database, or access an existing one
Once you have the appropriate information, the following PHP code will create a connection to MySQL and then select the appropriate database to perform queries on if the connection was successful.
$link = mysql_connect('localhost', 'root', 'pass'); //Connects to the database at "localhost" if(!$link) { die("Could not connect to database"); } //Exits the script if we can't connect mysql_select_db ('mydatabase', $link); //Selects the database "mydatabase" to run queries on
- localhost - There’s a chance that, if you’re using a remote web host’s server, the database server name will be something different but, more often than not, “localhost” will do fine. Enter your own username and password in place of “root” and “pass” too
- mysql_select_db (’mydatabase’, $link); - This simply selects the database that’s been created. In this case, a database “mydatabase”. Enter whatever name your website host has provided you with here, or check out the following tutorial on PHPMyAdmin (a database management application) to see how you can create your own databases and administrate existing ones
The above PHP code is all you’ll need to be able to run simple PHP queries on your new MySQL database. Whenever you call functions like mysql_connect(), you must ensure that, at some point earlier in the script, you have called mysql_connect() to create a valid database connection



