Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.

MySQL Connect

Before you can do anything with a MySQL database, you need to connect to it.

Here's how you do that: <?PHP
$DBhost = "mysql.mywebsite.com";
$DBuser = "my_username";
$DBpass = "my_password";
$DBname = "my_database_name";

$connect = @mysql_connect($DBhost,$DBuser,$DBpass);

if($connect){@mysql_select_db($DBname, $connect);}
We're setting the location of the database (your web hosting provider will tell you this) to a PHP variable called $DBhost.
We're setting your username for the database to a PHP variable called $DBuser.
We're setting your password for the database to a PHP variable called $DBpass.
We're setting the name of the database you wish to connect to (you might have more than one) to a PHP variable called $DBname.
We then perform the connection to the database host (using the $DBhost, $DBuser & $DBpass we set just set) and store a reference to it in a PHP variable called $connect. Notice the "@" symbol prefixed to the "mysql_connect()" function... that tells PHP not to print out any error messages if there is a problem establishing a connection to the database.
If the connection was established OK (if($connect)), we then select which database we want access to (using the $DBname we just set). Any "mysql_query()" which you perform following this, will now apply to this selected database.

Because you would usually only be working with one database per website, it's usually best to store this Database Connection script as a separate file, say as "DBConnect.php" and simply include it at the top of any PHP script that needs to connect to the database.
Example: <?PHP
Your database queries here!

What if there's an error connecting to the database?
We've already surpressed the PHP error message from being printed out (by using @), because we don't want our website users to see these ugly messages. But we still need a prettier way to let them know that something went wrong.
In the example above, we've already told PHP to do something if there's an error (include_once('DBError.php');), but what does this mean?
include simply means fetch whatever file we've asked for and 'merge' it into the current script.
Here's an example of the DBError.php file: <?PHP

<div class="header">Oops! Database Error.</div>
<div class="text">
This typically happens when the Database is undergoing maintainence, is restarting, or is overloaded.<br>
Database Reports: "<?PHP echo mysql_error(); ?>".<br>
MyWebsite.com have been notified, and would like to apologise for any inconvenience caused.<br>
Should the problem continue, then please contact them and let them know.<br>
Alternatively, you may wish to retry/reload the page!<br>
<div class="button" onclick="window.location.reload()" title="Retry/Reload Page">Retry/Reload</div>

If there is a problem connecting to your database, PHP will simply include the DBError.php file, and whatever is inside it will be executed (if it contains PHP code) and displayed (if it contains HTML code).
In our example, the DBError.php file contains both PHP & HTML code. The PHP code will include another 2 files... the websites' default header & footer, and between them some HTML code for the error message will be output.