Hello everyone, this is an "indepth" tutorial on how to setup an online high score board system. I mainly did this because around 2 weeks ago I searched everywhere to try and figure it out and noticed that no one ever made a tutorial (to my knowledge). By the way please try to excuse any grammar mistakes in this tutorial as I started it at 2 am and finished it at 4 am so my eyes are a bit heavy at the moment.
Enjoy
Credits go to Plystire for staying up all night helping me figure it out and doing most of the work. O and for just for being awesome .
1. Hosting a database
Start of by going to
http://freehostingphpmysql.com/
Sign up for a free account (on the bar at the top hit the "Signup" and choose the "Free hosting plan".)
Once signed up and logged into your account click the "Website Manager" tab on the top bar.
Off to the left hand side you will see a panel. Click the "MySQL" option.
Proceed by creating a new database (the Username and Password do not matter as long as you remember them)
Once created open the database page by clicking the blue hyper link of your database just below the database creation area.
A new page will open asking you to log in. Log in using your newly created databased
username and password.
Once you log in off to the left you will see your database name. Click on it and you will be brought to a new page to create tables. For this purpose (a highscore board) 1 should be enough. In the name box give it any name you want(this is case sensitive) and in the "Number of fields" box put 3(this is because we will be using name,score and an unique id).
Once you hit go you will be brought to a new page to insert settings for our 3 fields. For the ease of this tutorial fill out the three fields with the information in the picture below (the field names are case sennsitive) and hit save.
Once you do that go back to the original webpage that you signed up on at the begining of the tutorial (or optionally just click this
https://cp1.runhosting.com/website_manager.html). Get to the website manager tab again if your not already there and click the "Subdomain Manager" tab on the left panel.
In this section simply create a subdomain by choosing a name and "extension"(the choices in the drop down box). MAKE SURE YOUR REMEMBER IT! and hit "Add Subdomain"
Ok, were about half way done.
Next thing we want to do is click the "File Manager" tab on the left panel.
Click on the folder with your subdomain name.
For now minimize this window and open a text file.
2. PHP
Now its time for a bit of PHP . WARNING: THE SCRIPTS BELOW WILL HAVE TO BE SLIGHTLY EDITED (I will tell you what to edit)
<?php
$nam = $_GET["n"];
$sco = $_GET["s"];
//echo $nam . " got score " . $sco;
$con = mysql_connect("fdb2.runhosting.com","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//echo '<br>';
//echo 'Successfully connected to database!';
mysql_select_db("", $con);
$result = mysql_query( "INSERT INTO (,)
VALUES ( '" . $nam . "', '" . $sco . "' ) " );
mysql_close($con);
?>
First thing to edit, find this line (KEEP IN MIND THIS IS ALL CASE SENSITIVE)
$con = mysql_connect("fdb2.runhosting.com","","");
as you can see their are two blank quotes in this function. The first one takes the database's username and the second string takes the password.
So if my database name is 12345 and password is test123 I would fill the line in like this:
$con = mysql_connect("fdb2.runhosting.com","12345","test123");
The next one is simple, locate this line:
mysql_select_db("", $con);
In the blank quotes just put your database name
So if my database name is 12345 I would fill the line in like this:
mysql_select_db("12345", $con);
Ok and last thing for this script, locate this line:
$result = mysql_query( "INSERT INTO (,)
This line needs to be finished off with 3 things. The name of your table, the name of your name field, and the name of your score field.
if my table name is HIGHSCORE and my name field is Name and my scoe field is Score I would fill the line in like this:
$result = mysql_query( "INSERT INTO HIGHSCORE (Name, Score)
After you've done all of that save the text file as a .php script. (hit file>save as....> (choose all files for the file type) INSERTNAMEHERE.php)
Once you save it upload it to the file manager
Ok , were just about done with php. We have another small script to edit (this one does the actual returning of data)
Open a new text file and insert the following script and get ready to edit it.
<?
$con = mysql_connect("fdb2.runhosting.com","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//echo 'Successfully connected to database!';
mysql_select_db("", $con);
$result = mysql_query("SELECT * FROM ORDER BY DESC LIMIT 10");
while($row = mysql_fetch_array($result))
{
echo $row[''] . "|" . $row[''];
echo ";";
}
mysql_close($con);
?>
The first ones easy enough because we did this with the first script
Find this line:
$con = mysql_connect("fdb2.runhosting.com","","");
as you can see their are two blank quotes in this function. The first one takes the database's username and the second string takes the password.
So if my database name is 12345 and password is test123 I would fill the line in like this:
$con = mysql_connect("fdb2.runhosting.com","12345","test123");
Once again this next one is easy because we did it in the first script
locate this line:
mysql_select_db("", $con);
In the blank quotes just put your database name
So if my database name is 12345 I would fill the line in like this:
mysql_select_db("12345", $con);
Ok now its time to start paying attention because these next ones might get a bit confusing so pay close attention to the examples
Locate this line
$result = mysql_query("SELECT * FROM ORDER BY DESC LIMIT 10");
Now a space after the word FROM we need to insert the table name and after the word BY we need to insert the field name we used for our score
if my table name is HIGHSCORE and my score field is Score I would fill the line in like this:
$result = mysql_query("SELECT * FROM HIGHSCORE ORDER BY Score DESC LIMIT 10");
(as a spare note this code only grabs the top 10 scores, to changes this change this line
)
Finally locate this line
echo $row[''] . "|" . $row[''];
in the first [''] box we want to insert the name of our name field between the '' and in our second [''] we want to insert the name of our score field
If my name field's name was Name and my Score field's name was Score my line would look like this:
echo $row['Name'] . "|" . $row['Score'];
Note: between the name and score there will be a | and a ; after each person's score to make things easier to separate in DBPro (so if the user's name was Sean and his score was 5 it would come out like this: Sean|5; )
After you've done all of that save the text file as a .php script. (hit file>save as....> (choose all files for the file type) INSERTNAMEHERE.php)
Once you save it upload it to the file manager
We can now run a quick test:
Go to your subdomain's url and at the end of it add a / the name of the first script we made and ?n=MyName&s=15000000
load the page ( the page will be white,just ensure that it was loaded all the way)
(mine would look like
http://highscore.mygamesonline.org/dbpost.php?n=MyName&s=15000000 )
after that go to your subdomain's url and at the end of it add a / and the name of the second script we made
(mine would look like
http://highscore.mygamesonline.org/dbscript.php )
You should then see the high score that you put in with the first url (if you don't then go back and ensure you followed the tutorial correctly.
3. DBPro
Ok, its time to fire up DBPro and create a new project.
(this part of the tutorial will be very brief)
Insert these two functions in your new project:
function download(from$,to$)
if file exist(to$) then delete file to$
load dll "urlmon",1
failed=CALL DLL(1,"URLDownloadToFileA",0,from$,to$,0,0)
delete dll 1
endfunction failed
function parseString(t$, look$, num)
ct=0
r$ = ""
last = 0
for i = 0 to len(t$)
if (mid$(t$,i) = look$)
if ct = num then r$ = right$(left$(t$,i-1), len(left$(t$,i-1))-last)
t$ = right$(t$,len(t$)-i)
if ct = num then exit
inc ct
last = i
endif
next i
endfunction r$
4. How to use the new functions
Now that we have our new functions in its time to put them to use.
How to send a new high score:
To send a new high score you will be calling the download function.
The first parameter of this function will take our website's subdomain along with the / the name of the first script we made and ?n=INSERT NAME HERE&s=INSERT SCORE HERE
The second parameter of this function is where we want our text file to download to (this is the file that will hold our data when we retrive it)
Example bewlow using a string (Name$) to hold the user's name and a variable (Score) to hold the user's score.
fail = download("http://highscore.mygamesonline.org/dbpost.php?n="+Name$+"&s="+Str$(Score),"C:\dbhighscore.txt")
How to return high scores:
This method is pretty straight forward and we will once again be using the download function but this time we will also use the parse function.
When returning data the first parameter of the download function will take your subdomain's url and at the end of it add a / and the name of the second script we made.
The second parameter of this function is where we want our text file that contains our data to download to.
fail = download("http://highscore.mygamesonline.org/dbscript.php,"C:\dbhighscore.txt")
The parse function takes the string to parse,the letter/character were looking for, and how far to look. The way we setup our php script requires specific inputs for this function.
Since this is more of a straight forward thing I've posted example code below on how it should be used:
fail = download("http://highscore.mygamesonline.org/dbscript.php,"C:\dbhighscore.txt")
open to read 1, "C:\dbhighscore.txt"
read string 1, s$
ct=0
a$="A"
while (a$ <> "")
a$ = parseString(s$, ";", 0)
inc ct
print a$ ` Print our record to screen
s$ = right$(s$, len(s$)-len(a$)-1)
endwhile
close file 1
Now the end is finally in sight. Below is just some test code to make sure it all works (this is running off of my database, to test yours you should edit the first parameter in the download function)
fail = download("http://highscore.mygamesonline.org/dbpost.php?n=Sean&s="+str$(rnd(15000000)),"C:\dbhighscore.txt")
fail = download("http://highscore.mygamesonline.org/dbscript.php","C:\dbhighscore.txt")
print "Failure? "+str$(fail)
open to read 1, "C:\dbhighscore.txt"
read string 1, s$
ct=0
a$="A"
while (a$ <> "")
a$ = parseString(s$, ";", 0)
inc ct
print a$ ` Print our record to screen
s$ = right$(s$, len(s$)-len(a$)-1)
endwhile
close file 1
wait key
end
function download(from$,to$)
if file exist(to$) then delete file to$
load dll "urlmon",1
failed=CALL DLL(1,"URLDownloadToFileA",0,from$,to$,0,0)
delete dll 1
endfunction failed
function parseString(t$, look$, num)
ct=0
r$ = ""
last = 0
for i = 0 to len(t$)
if (mid$(t$,i) = look$)
if ct = num then r$ = right$(left$(t$,i-1), len(left$(t$,i-1))-last)
t$ = right$(t$,len(t$)-i)
if ct = num then exit
inc ct
last = i
endif
next i
endfunction r$