Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[TUT] Adding MySQL compatibilty


Peaverin
 Share

Recommended Posts

This is a very simple MySql connection tutorial. It just allows your server to connect to a MySQL database and I created some examples to see how you can use this. You can do cool things with MySQL to interact with your webpage: level rankings, players online and record, etc… It's even possible to edit the source of your project so everything is saved in MySQL but that would take a lot of time. I just did this because I wanted to try if game server-webpage server interaction was possible. Even web page registration would be possible and it wouldn't take a lot of time  (inserting a username and a password in a table and then making the server check that table every x time and create the account if there's a new username in the table).

Okay let's start. You'll have to know how to set up a MySQL database and create tables.
Everything is server side.
1-First of all you'll have to go to project references and check Microsoft Activex Data Objects Library 2.8.
If it doesn't allow you to do it you'll need to install msado28.tlb.

2-Now we have to establish connection between server and MySQL server. Usually you would put this when the server loads everything, in Sub InitServer():
```
''Set MYSQL Connection
Set cn = New ADODB.Connection

''you don't have to put quotes
cn.Open "Driver={Mysql ODBC 3.51 Driver}; Server=YOURSERVERIP;port=YOURSERVERPORT; database=DATABASENAME; user=DATABASEUSER; password=DATABASEPASSWORD; option=3;"
Call SetStatus("Connecting to MYSQL Database...")''this is optional
End Sub

```

If you establish the connection and then you wait a lot of time to make a query maybe the MySql connection has finished because of the MySql timeout. If that happens server will crash and we don't want that. To avoid that, we can either start the connection everytime we want to send a query or just start the connection every x time. I prefer the second one because if there are lots of querys then the server will get laggy.
To do that we just have to **create a timer with interval of 64000ms anywhere and put the same code you put earlier.**

Test this and if your database works properly and you put the data correctly then you'll be connected.

3-Congratulations, now you can send any query by doing this:
```
Dim strsql As String
''Mysql update players online
''you set strsql as the query you want to send (the sintaxis is the same of a mysql query)
    strsql = "update table set data1=" & somevariableforexample & " where number=0;"
    Set rs = cn.Execute(strsql)
    Set rs = Nothing

```
4-Now, you can retrieve some code by doing using rs. rs is a reference to what you've selected in the query. I'm sorry but I can't explain this better since I'm Spanish and don't speak English very well. Here you have an example:

```
   strsql = "select * from players where number=0"
    Set rs = cn.Execute(strsql)
```In this case, i've selected the row when number=0, so rs is now a reference to that row. I can now retrieve some information of that row by using rs!column. For example:
```
rs!somecolumn
```will give you the data of the selected row(where number=0) in that column.

5-You can do a lot of things with this, like, for example, saving the online players and record in a table, or lvl ranking, or even a market using the database… It's all up to your imagination. Feel free to pm me or post here if you need some help.

6-You can also set up a webpage and connect to the same database so you can change data between the webpage and the server. I won't explain this part. I don't even know how to do it but googling "how to connect to mysql database in php" gives me around 13.700.000 results so I think it's no trouble at all.

I hope you enjoy this little tutorial that can be used to make very useful systems. Every ORPG nowadays has got website interaction, so I'd recommend everyone to have systems around this on their games.
Link to comment
Share on other sites

It's nice of you to share this. But, if you are releasing a tutorial, it'd be best if you go all in or don't do it at all. You've told us how to connect to a db and query it. But, you haven't told us how to retrieve data after querying.
Here:```
Set rs = cn.Execute(strsql)
```the varaible **rs** has never been declared and you've never told us what it references to.

btw, why is Microsoft Remote Data Object 2.0 needed in the project? Is it ever referenced? I'm pretty sure you can use ADODB in Microsoft Activex Data Objects Library 2.8 project to do all you'd event want to do.
Link to comment
Share on other sites

You're right. I haven't explained some things because I don't even know them. That rs is a reference to what you've selected in the query. I'm sorry but I can't explain this better since I'm Spanish and don't speak English very well. Here you have an example:
```
strsql = "select * from players where number=0"
Set rs = cn.Execute(strsql)
```In this case, i've selected the row when number=0, so rs is now a reference to that row. I can now retrieve some information of that row by using rs!column. For example:
```
rs!somecolumn
```will give you the data of the selected row(where number=0) in that column.

And you're also right, the Microsoft Remote Data Object 2.0 is not needed, sorry for the missunderstood, I'm editing the main post now.
Link to comment
Share on other sites

  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...