x


Is it possible to send and recieve data from/to a MySql server?

Hi everyone! I want to create a PHP website with a MySql server, where users can register, login, and then get "coins" (like an arcade game). Then users can use those "coins" to play 3D webgames made with Unity.

Can I get the Unity webgames communicate with the MySql server, in order to send and retrieve data like username, login, number of available coins, etc?

more ▼

asked May 26 '10 at 08:30 AM

DanjelRicci gravatar image

DanjelRicci
380 17 20 31

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You should use standard HTTP Get or Post requests, via Unity's WWW class, to send requests to your PHP scripts, and read the responses.

The PHP scripts on your server would then communicate with the Database, and perform tasks like checking whether a username is available, creating new accounts, logging in, retrieving account data, etc.

You should consider it in a similar way to how you would build it as if it had a simple web page based form interface, but instead of typing in the form values into text boxes, you pass them as values via scripts. The returned page can then contain the data in whatever format you want, for your game to process.

For more information, see this Question:
How can I send and receive data to and from a URL, i.e. server side scripts, web services, etc?

more ▼

answered May 26 '10 at 01:27 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Thank you very much, this sounds really helpful and easy too. ;)

May 26 '10 at 05:05 PM DanjelRicci

the site is dead.. can someone help me again?

Aug 27 '12 at 02:20 PM fadilRaditya
(comments are locked)
10|3000 characters needed characters left

Yes you can with cs

Just use mysql connector for cs and import the MySql.Data.dll to your project.

Here is an example mysql connect script:

using UnityEngine;
using System.Collections;
using MySql.Data.MySqlClient;
using System;


public class mysqlconn : MonoBehaviour {
static string row ="";
    // Use this for initialization
    public string Start () {

    string myConnectionString = "SERVER=localhost;" +
                            "DATABASE=yugioh;" +
                            "UID=root2;";
        MySqlConnection connection = new MySqlConnection(myConnectionString);
        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT * FROM accounts";
        MySqlDataReader Reader;
        try {
        connection.Open(); 
        Reader = command.ExecuteReader();


while (Reader.Read())
{
    for (int i = 0; i < Reader.FieldCount; i++)
    row += Reader.GetValue(i).ToString() + ", ";
}
        }     

        catch (Exception x) {
            Debug.Log(x.Message);
            return x.Message;
        }
connection.Close();
return row;

    }

    // Update is called once per frame
    void Update () {

    }
}
more ▼

answered May 26 '10 at 08:43 AM

Tobias gravatar image

Tobias
378 50 54 66

No no no! This would create a huge security hole! In the scenario that Mr. Drayton describes, you should never allow the client on the end user's machine to directly connect to your DB! (see my answer)

May 26 '10 at 01:22 PM duck ♦♦

plus in a webplayer you cant use .dll files...

Jun 15 '10 at 02:50 PM ikriz

hi, Tobias Windberg can u please send me the exact procedure or code to make sure run properly without any loop holes

Sep 18 '12 at 12:05 PM murthyveera
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x212
x131
x102
x88
x11

asked: May 26 '10 at 08:30 AM

Seen: 8861 times

Last Updated: Sep 18 '12 at 12:05 PM