2

I'm developing a WebGL app with Unity 2018.

I need to access a MySQL database hosted on the same server.

My questions is: can I use C# and MySqlConnector (solution i prefer), or I have to use instead web services calls ?

EDIT: it seems it's not either possible to use/consume web service in WebGL build, because System.Web.Services it's not accessible.

Thanks

1
  • maybe this link can help you Commented Mar 15, 2019 at 18:05

3 Answers 3

3
+300

The general way for doing this (both for security reasons and it's just all round a more standard way from the unity side of things) is that you simply host the server side database interaction pages within php and then make use of either the WWW or UnitWebRequest classes to actually utilize this.

You can fairly easily get back Json formatted data to work with on the client side

A fairly old (though still kinda relevant) tutorial/guide for something along these lines can be found Here

Generally, the idea is though - Game makes a request to PHP using an encrypted key of some kind for validation, PHP does the database work and then fires back some data usually in Json format

Sign up to request clarification or add additional context in comments.

1 Comment

my database has local and i want to communicate with database without php and directly from unity and c#. i used MysqlClient and mysqlConnector/.Net but it doesn't support webgl. i am making a local application and security doesn't matter to me. any idea?
2

You can make a websocket. The server who will host the WebGL app will have a websocket server, who will do the MySQL communication and return the results. And the WebGL app(the unity one) connects to this websocket using a Auth Token(To ensure that is the WebGL connecting, not someone else) and get the results from the queries there

Comments

1

You might already have your answer but i have done it few weeks ago and its pretty easy. I am posting both php and c# code below, have a look:

C# Code

 WWWForm form = new WWWForm();
WWW www = new WWW("url of file location on server goes here", form);
yield return www;
Debug.Log(www.text);// this should return "successful connection"

PHP Code

replace '123' with required fields

 <?php
$servername = "123";
$server_username = "123";
$server_password = "123";
$dbName = "123"; 

//Connection
$conn = new PDO ("mysql:host=$servername;dbname=$dbName", $server_username, $server_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Check Connection
if(!$conn){
    die("Connection failed.");
}else{
echo "Connection Successful";
} 
?>

Hope its helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.