Create a Free Website

PHP MySQL
Interactive Website Design


Connecting to the MySQL Server

Before you can perform any operation on a database you must connect to the MySQL server.

The syntax for performing this operation is shown with variations below.



Note: If you have accessed this web page via a search engine, you should go back and start at the Beginning.

This tutorial is designed to be viewed and executed in sequence.

Learn to build your database right on your PC and Export it to your website.

TRY This Website Builder FREE!

No Credit Card Needed!
Create beautiful,professional websites
WITHOUT Learning to Code!!

Use this Next Generation Builder
to make blogs, portfolios or
full blown websites.
No Experience Needed!

SquareSpaceSquareSpace

Start Your Website Now!SquareSpace

Professional websites
Without the HASSLE!



Connecting to the MySQL Server

This is just the basic syntax:
$link = mysql_connect("localhost");


This would be used if coming from a log in form:
<html>
<head><title>Connect Server</title></head>
<body>
<?
$link = mysql_connect("localhost",$_POST['username'],$_POST['password']) or die(mysql_error());
print "Successfully connected.\n";
mysql_close($link);
?>
</body>
</html>


In the code shown above the red and blue syntax is all one line.

The blue portion is for error trapping. (See below).

A simplified version of the code is shown below. It does not include the username, password option.

This code would be used on your PC for site development if you did not specify a username and password when you set up the MySQL server on windows:
$link = mysql_connect("localhost");


Note: Your webhost will require you to include server name and port settings in your connect statement. The configuration shown below will work with most servers:
$link = mysql_connect(servername.com:3306,username,password);


The code shown below is used when you require your users to log in to access form processing.

If log in is not required just place the actual values in the connect query statement.

$link = mysql_connect("servername.com:3306",$_POST['username'],$_POST['password']);

IndigoAMPP/HTMLPad 2010 Users

The IndigoAMPP server is set up to allow access and operations for a user named root without a password.

test_db.php

This is the configuration we'll use on our scripts.

<html>
<head><title>Connect Server</title></head>
<body>
<?
$link = mysql_connect("localhost","root","") or die(mysql_error());
print "Successfully connected.\n";
mysql_close($link);
?>
</body>
</html>

To run the script:
HTMLPad 2010 users:
Make sure the IndigoAmpp server is running (Check for icon in system tray)
Use normal File - Open to load test_db.php into the editor
Click Preview on bottom of editor window.

When you run the script you should see a white screen and the words 'Successfully connected' in the upper left corner.

If you DON'T:
Did you check to see if the server is running? Do you know how?
Did you save the scripts in c:\indigoampp\apache-2.2.11\htdocs folder or a folder inside that folder?
Did you set up Preview - Mappings as instructed in my tutorial?
If you missed any of these steps, you need to STOP and Read My Tutorial on setting up HTMLPad.

If you don't have HTMLPad 2010,
you will have to open your browser and manually type the url as:
http://localhost/test_db.php.



If you were successful, you are ready to Create a Database


Sample Login Form

<form method="POST" action="connect_server.php">
Enter Username: <input type="text" name="username" size="20">
Enter Password:<input type="password" name="password" size="20">
<input type="submit" value="Submit"><input type="reset">
</form>

Error Trapping

To save you from constantly visiting the error log file to find problems in your code , you can make use of a simple error reporting function provided by the mysql server. Using it with the PHP die function will help to pinpoint problem areas in your code.

The code shown below could be appended to the end of any mysql function statement.
or die(mysql_error());


Closing the Connection

It is good practice to break the connection with the mysql server when operations have ceased. This simple procedure is accomplished with the line of code shown below:
mysql_close($link);

Building Your Database on Your PC

In order to build your database on your PC and later Export it to your website requires setting up a localhost server on your PC and finding an HTML editor that will sync up with the server.

If you use Windows, the process of setting up and running a localhost so that you can test and edit scripts and build databases is very easy.

I recommend the IndigoAMPP web server and the HTMLPad 2010 HTML editor.

I recently installed IndigoAMPP for Windows on my Vista system and it ran on the first try after installation. Setting up HTMLPad 2010 to work with it is also an easy process.

I created a little tutorial that shows the whole process including how to start the server after you install it. Go to Tutorial
Note: The server is FREE and you can try the HTML editor for 30 sessions or 30 days free. If you can't learn to build your database in that amount of time, don't buy the editor. NO RISK!!

Download the Scripts

The Birthdays Database management files can be downloaded in a zip file.

If using IndigoAMPP download to c:\indigoampp\apache-2.2.11\htdocs.

Extract there and you'll have a birthdays folder inside your htdocs folder. Run the scripts from there.

The package contains an integrated db management system, with a simple interface.

This Instruction file is included in the download.
Download birthdays_db.zip

MySQL Tutorial

To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com