This is an old revision of the document!


Creating the Database

The steps for setting up your database will depend on the setup of your server, but we'll briefly look at the steps for cpanel users. After logging in you are looking for the databases section:

If the wizard is available, then you may as well use it (2nd option from left). Just be sure to notedown any names or passwords that you enter. The stages for me were:

  1. Choose a name for the database (keep it short but meaningful, like “mmo”). Note there may be a prefix linked to your account - like “account_”. If there is then your database name is actually “account_mmo”.
  2. Create a user and a password. Again the username may have a prefix set for it, so be sure to note down the whole name.
  3. Set privileges. The recommended settings are shown in the image below.

Creating Tables

phpMyAdmin is a great interface for working with your database, so hopefully you have it installed on your server. If you return to cpanel, then you should be able to find the icon to click on.

Depending on how your server is setup, clicking on the icon may automatically take you into phpMyAdmin as the administrative user. If you are presented with a login screen, then use the database username and password that you just created.

We are going to start with 3 tables:

  • user details - the people using your game
  • monster details - the types of monsters in your game
  • spawned monsters - details for each monster that actually exists in your game

Creating a Database Connection File

To make things easier for ourselves, We can put the code to connect to the database in a separate file. Copy and paste the code below into a new file, changing the database name, user name, and password to your own settings. $dbhost will stay as locahost in most cases. Save it as dbinfo.php.

<?php
 
// Pre-define connection to the MySQL database, please specify these fields.
$dbhost = "localhost";
$dbdata = "account_database";
$dbuser = "account_username";
$dbpass = "password";
 
// Connect to predefined MySQl database. 
$db = new PDO( "mysql:host=$dbhost; dbname=$dbdata", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND =>  "SET NAMES utf8") );
 
// set the error reporting attribute to Exception.
$db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );
 
 
?>