EOSERV Wiki > Page: The Web And EOSERV PHP MySQL Tutorial

The Web And EOSERV PHP MySQL Tutorial

Over this tutorial I will be going into depth on how you would go about drawing your EOSERV Database into your website (Only supports PHP [Apache] / Mysql) To start I will be going over a few key concepts with you guys.

The first thing you're going to have to master is connecting to your local database to actually pull the information into your wesbite. Below is how you would do exactly that, just like the config.ini file, you're going to need a host name, a username and a password in order to connect to your mysql database

<?php

//Connecting to your local database
$host="localhost";
$username="eoserv";
$password="eoserv";
$dbname="eoserv";
$con=mysql_connect($host,$username,$password);
if ($con) //if you have successfully connected to your database
{
//execute this code
}
else
{
die ('could not connext: '. mysql_error()); //print the error message
}
?>

Take this code and save it as index.php or something similar just to check to see if you have everything set up correctly. If you are getting a mysql error, the most likely cause for this is either your mysql server isn't actually running or you may have miss spelt something when you were setting up the variables to connect to your local database.

On the flip side to this if you have successfully connected, continue with this tutorial:

Once you are connected, there's quite a lot of possible ideas that you can bring to life through the use of Structured Query Language or as most programmers refer it to SQL. PHP works with MySQL very well with a lot of pre-defined functions to handle and browse through even the most complex of databases, so let's start with some simple SQL queries for possible ideas you may want to incorporate with your website using $sql as the php variable to store the SQL Query string:

Top 100 SQL

Step 1: Selecting every character from the database $sql="SELECT * FROM Characters"; //this one snippet of code selects every single character stored in your database#

Step 2: Ordering your selection by level $sql="SELECT * FROM Characters ORDER BY Level DESC"; //ordering the character selection by level (Descending)

Step 3: The PHP Code <?php

//Connecting to your local database
$host="localhost";
$username="eoserv";
$password="eoserv";
$dbname="eoserv";
$con=mysql_connect($host,$username,$password);
if ($con) //if you have successfully connected to your database
{
//The following code is to be executed on database connection
mysql_select_db($dbname);
$sql="SELECT * FROM Characters ORDER BY Level DESC"; //From step 2 of the tutorial
$result=mysql_query($sql); //Executing the query to return the result from the database
while ($row=mysql_fetch_array($result)) //while there are still rows to return from the database
{
echo("Character name: ".$row["name"]." level: ".$row["level"]."
"); //this prints the character name, and his or her level in descending order so the player with the highest level will be printed first.
}
}
else
{
die ('could not connext: '. mysql_error()); //print the error message
}
?>

NOTE: This code does not limit the amount of results that are returned and therefore printed so the following step is needed to only get the top 100 players from the database

Step 4: Limiting how many results are returned Append the code from Step 3

<?php

//Connecting to your local database
$host="localhost";
$username="eoserv";
$password="eoserv";
$dbname="eoserv";
$con=mysql_connect($host,$username,$password);
if ($con) //if you have successfully connected to your database
{
//The following code is to be executed on database connection
mysql_select_db($dbname);
$sql="SELECT * FROM Characters ORDER BY Level DESC"; //From step 2 of the tutorial
$result=mysql_query($sql); //Executing the query to return the result from the database
$count=0; //initializing the $count vairable.
while ($row=mysql_fetch_array($result) && $count<100) //while there are still rows to return from the database and while the count variable is less than 100.
{
echo("Character name: ".$row["name"]." level: ".$row["level"]."
"); //this prints the character name, and his or her level in descending order so the player with the highest level will be printed first.
$count++; //incrementing the count variable by 1. if $count=1, $count now equals 2.
}
}
else
{
die ('could not connext: '. mysql_error()); //print the error message
}
?>

and that's it for your top 100 code. If anyone would like any more help with this tutorial feel free to comment and I'll do my best to help you. I'll be adding to this tutorial by demand and maybe a few other things if I have the time to add them and if they're relevant :) Thanks for reading! P.s If I have got anything wrong, feel free to point out, I'd be greatful :) Thanks again!

EOSERV Wiki > Page: The Web And EOSERV PHP MySQL Tutorial