EOSERV Forum > Programming > The Web And EOSERV. [PHP/MySQL Tutorial]
Topic is locked.
Page: << 1 >>
The Web And EOSERV. [PHP/MySQL Tutorial]
Author Message
Post #115728 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 yourwebsite(OnlysupportsPHP[Apache]/Mysql)TostartI will be going over a few key concepts with you guys. 

By the end of this tutorial you should be able to output something that resembles the image below

The first thing you're going to have to master is connecting to your local database to actually pull the informationintoyourwesbite.Belowishowyouwoulddoexactly 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.Ifyouaregettingamysqlerror,themostlikely 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 ofStructuredQueryLanguageorasmostprogrammersreferitto 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 websiteusing$sqlasthephpvariableto storethe SQLQuerystring:


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"]."<br>"); //this prints the character name, andhisorherlevelindescendingordersotheplayer 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 stepisneededtoonlygetthetop100playersfrom 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 LIMIT 100"; //From step 2 of the tutorial + the limitations Credits: Arcitex

$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 and while the count is less than 100.

{

echo("Character name: ".$row["name"]." level: ".$row["level"]."<br>"); //this prints the character name, andhisorherlevelindescendingordersotheplayer with the highest level will be printed first.

}

}

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 domybesttohelpyou.I'llbeaddingtothis 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!

---
If money doesn't grow on trees, then why do banks have branches?
13 years, 25 weeks ago
Post #115731 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

I couldn't get it to work, it connects to the db but doesnt echo the values



---
Beware of your thoughts, they become your words. Beware of your words, they become your actions.
Beware of your actions, they become your habits. Beware of your habits, they become your character.
Beware of your character, it becomes your destiny.
- Unknown
13 years, 25 weeks ago
Post #115732 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

did you get that code from Step 4?

---
If money doesn't grow on trees, then why do banks have branches?
13 years, 25 weeks ago
Post #115733 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

yes, from step 4

---
Beware of your thoughts, they become your words. Beware of your words, they become your actions.
Beware of your actions, they become your habits. Beware of your habits, they become your character.
Beware of your character, it becomes your destiny.
- Unknown
13 years, 25 weeks ago
Post #115734 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

could you paste your php code into the paste bin then link me to it please :)

---
If money doesn't grow on trees, then why do banks have branches?
13 years, 25 weeks ago
Post #115735 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

https://tehsausage.com/paste/7a53

sure thing, only thing changed is username and password

---
Beware of your thoughts, they become your words. Beware of your words, they become your actions.
Beware of your actions, they become your habits. Beware of your habits, they become your character.
Beware of your character, it becomes your destiny.
- Unknown
13 years, 25 weeks ago
Post #115736 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

Found my error, I didn't put brackets around both comparators for the while statement. 

it should be: while (($row=mysql_fetch_array($result)) && ($count<100)) Thank you for the comment :)

Updated the tutorial 

---
If money doesn't grow on trees, then why do banks have branches?
13 years, 25 weeks ago
Post #115737 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

thanks DS it works great :)

---
Beware of your thoughts, they become your words. Beware of your words, they become your actions.
Beware of your actions, they become your habits. Beware of your habits, they become your character.
Beware of your character, it becomes your destiny.
- Unknown
13 years, 25 weeks ago
Post #115738 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

You're welcome, if there are any more web interface requests for the eoserv database, feel free to post and I'll try and help out as much as I can :)

---
If money doesn't grow on trees, then why do banks have branches?
13 years, 25 weeks ago
Post #115743 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

I would like to replace Player Name: with the rank ie

1: somelayer 58

2: someorther player 55

etc


how can I do that? 

---
Beware of your thoughts, they become your words. Beware of your words, they become your actions.
Beware of your actions, they become your habits. Beware of your habits, they become your character.
Beware of your character, it becomes your destiny.
- Unknown
13 years, 25 weeks ago
Post #115745 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

while (($row=mysql_fetch_array($result))&&($count<100))
{
$count++;
echo($count.". ".$row["name"]." ".$row["level"]."<br>");
}

should do it :)

---
If money doesn't grow on trees, then why do banks have branches?
13 years, 25 weeks ago
Post #115746 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

thanks again

---
Beware of your thoughts, they become your words. Beware of your words, they become your actions.
Beware of your actions, they become your habits. Beware of your habits, they become your character.
Beware of your character, it becomes your destiny.
- Unknown
13 years, 25 weeks ago
Post #115756 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

Didn't look over the whole thing, but at a glance one really bad thing is the way that you limit it to 100 characters. You should let MySQL do that simply by adding a limit to the query:


SELECT * FROM Characters ORDER BY Level DESC LIMIT 100

13 years, 25 weeks ago
Post #115758 Re: The Web And EOSERV. [PHP/MySQL Tutorial]

Thanks for that piece of information :) Tutorial Updated!

---
If money doesn't grow on trees, then why do banks have branches?
13 years, 25 weeks ago
Page: << 1 >>
Topic is locked.
EOSERV Forum > Programming > The Web And EOSERV. [PHP/MySQL Tutorial]