PHP MySQL
Create & Test MySQL Database

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.

 

This is 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']);

Apache Server Users

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

Copy & Save as: 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 2014 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.15\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.

Using Other Editors

If you don't have HTMLPad 2014,
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

You won't use this form in the tutorial.

<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);

MySQL Tutorial

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