Unreal Engine 4 with MySQL database connection

Hello unreal engineers,

I have some questions regarding MySQL integration to Unreal.

I want to use MySQL database for my game to store player stats, items and position in the world. I know it’s possible to connect MySQL with C++ but you have to use some dll’s i think. C++ connector for MySQL or something like this it was called. But I don’t know if to integrate the dll into the Game project and store everything in the game code (mysql username, password) or to make C++ send http requests to php masterserver. How do you think it’s better?

Usually you send requests to php master server, and the server does eveything with the databases.

Do you know what I should search for in google? I tried searching how to create a masterserver, but there was only for unity. I might try doing something with console app in c++ for start, but I don’t know where to start.

Look for generic C++ REST request tutorials. You can use all generic stuff with UE4 so this should be easily done. One good web server you can get for testing is XAMP. If you eed any more help I can try and put together a quick example when I get to my PC.

I have almost a year of experience with XAMPP, I know PHP a little (making registrations, logins, and other stuff like this). I just need to know how to setup the masterserver, and find out how to Send POST/GET requests. I google for 3 hours now and can’t find anything…

I too need to a pointer to the “Send Post/Get” requests. Or maybe someone can help push me in the right direction to explain how other games using UE4 intend or have handled growth where it has made sense to keep user data/inventory/leaderboard stats and such near the users instead of a central server.

If you’re good with programming you could grab an embeddable web server like Mongoose and use it. But the short answer is no, you can’t easily add HTTP request support to your game- unless I’m mistaken and this stuff is already added in the engine. Even though it SHOULD be as simple as 1-2-3, there is some C programming that needs to be done just for the basics, sadly.

Why YOU should embed a web server in your game engine

Maybe someone can write a small plugin, in a matter of days even. An embeddable webserver means basically that you can in theory have the whole thing, a highscores or game entity list right in your browser window for editing, with the data server also there on your own PC. Interesting stuff. I’m doing something similar so I see that this is possible to code right now.

That depends on where you need that MySQL connection, if on server then i dont know why you would torture yourself with PHP, if client thats reasonable you need to think about security as anyone qill be able to reproduce HTTP request. User and Password can be stored in INIs and you can operate them from GConfig

You aren’t going to want to have the SQL information in the client at all. That is a big security risk. What you need is either a PHP server (which would get really slow if you are updating more than once every couple of minutes with a bunch of players), or a dedicated server which has that information.

Is this a multiplayer game where you can interact with other players? If so, I’d honestly not recommend going the PHP route at all. Reason being, you are going to want to sanitize and validate all input from the player. Doing this in PHP would be insanely slow. You’d be much better off writing a dedicated server and referencing the MYSQL API (http://dev.mysql.com/doc/refman/5.0/en/c-api.html) to save database information on the server computer.

If this is a single player game that just has data saving to a webserver, you can go the PHP route. You’ll want to use the built in HTTP request features already built into the engine. They can be found at Engine/Source/Runtime/Online/HTTP.

MySQL on clinet is out of the question i don know why we even discussing that, it’s clucky thing to do… i mean you would need user to install and setup MySQL server :stuck_out_tongue: If you want to store data on client SQL style you could use SQLite which is server-less solution. But doing HTTP interface (reminder, it does not need to be PHP) for server-side locally is clucky too as you on the server where it’s safe and HTTP and scripting solution there just pointlessly buttlenecking

Sorry, it is written in German in my blog and also in C# but C++ is very similar to that. Here is an example how to do a system like this: C# - PHP - MySQL
http://www.3hmonkey.com/index.php/c-login-system/

He’s not talking about having MySQL on the client, he’s talking about communicating with MySQL directly from the client, even if MySQL itself is on a server. That’s not standard practice, because it’s insecure.

I do agree about PHP though; (to me) it’s just a lot more work, so I would use the MySQL API directly on the server in a thread instead.

Go grab a library for C++ that can access MySQL databases, I will have a look later on if I get time:

Create a layer in your game:

MySQLDbContext.h


// #include the db library

class MySQLDbContext
{
public:
    MySQLDbContext();
    void Query();
private:
    LibraryInstance* dbLib;
}

MySQLDbContext.cpp


// #include MySQLDbContext.h

MySQLDbContext::MySQLDbContext()
{
    dbLib = new LibraryInstance();
}

void MySQLDbContext::Query( std::string query )
{
    dbLibResultType result = dbLib->whateverthefunctionis( query );
}

Now you want some separate repositories for different operations you’re going to send:

MySQLSaveGameRepository.h


// #include MySQLDbContext.h

class MySQLSaveGameRepository
{
    public:
        MySQLDbContext();
        int SaveGame();
    private:
        MySQLDbContext* dbContext;
}

MySQLSaveGameRepository.cpp


// #include MySQLSaveGameRepository.h

MySQLSaveGameRepository::MySQLDbContext()
{
    dbContext = new MySQLDbContext();
}

int MySQLSaveGameRepository::SaveGame( SaveGameObject )
{
    std::string query = // Construct your MySQL query here
    dbLibResultType result = dbContext->Query( query );
    return dbLibResultType.ID;
}

With all the above in place, you can really just search for a library in C++ that works well for you! Screw PHP, surely you don’t want that layer…

Now if you do want that layer… instead of grabbing a db library, grab a HTTP POST AND GET request library… please apply the same principles here, you will prepare your POST message that will use the repository to send off to a specific URL on your PHP server, the PHP server will take the $_POST parameters and add to the db… visa versa with getting information from the database.

1 Like

But I heard that it’s not safe to keep database connection in the client.

He’s just showing you how to do it, on any side… be it client or server.

The engine provides an abstract layer for performing HTTP requests. In fact, we have a number of systems that rely upon it (Launcher, analytics, etc.) The classes in question are HTTPRequest/HTTPResponse. They are created via the HTTP module manager which manages the object lifetimes and ticking. We also provide some handling classes. During the last set of UE4 Mini DevCon talks, I spoke on this topic. The slides for it should be up soon and will help point you in the right direction.

BTW, having the MySQL code in the client is a security risk for your game. Any hacker can destroy or tamper with your database. If you are using a solution like that, at least only provide the access to the database from a dedicated server. The rest of the time the code should be compiled out.

Yep that’s exactly what I wanted to say in the above topic. That’s why I wanted to send data to php pages and then save it in the database, yes it will be a little slower but at least safer. I figured out how to do it actually, I did it in C++ console app first and I guess it’s the same way in UE4. Thanks everyone for the help provided!

I dunno if it will be safer, than simply calling code from dedicated server.
You can create separate module like YourGameSever, and put all server specific code here, and compile it only with dedicated server.

PHP layer seems to me, like to much fuss to maintain.

I have also chosen the php solution. I don’t know how good it is if thousands of people would try to send/receive information from it at the same time… but right now it is pretty fast.
I guess for saving stuff like inventory or stats it is ok… that is if you are not saving every tick of course.