0
\$\begingroup\$

I am working on database connectivity in unity.When I run my project I am able to access my database(retrieving and inserting data into database).But the problem is that when I take the android out the project I am having not able to accessing my database on my mobile android device. It seems that it doesn't read my database or I don't know my database is not included upon build.

Below code is working fine. I am using MySQL

string loginCode="http://localhost/unity_db/login.php";
string registerCode="http://localhost/unity_db/register.php";

public string username="";
public string password="";
public string output="";


void Start () {
    Debug.Log("print");
}
// Update is called once per frame
void Update () {
}

void OnGUI()
{
    GUI.Window (0, new Rect (Screen.width/4,Screen.height/4,Screen.width/2,Screen.height/2-5), LoginWindow, "Login");

}
void LoginWindow(int windowID)
{
    GUI.Label (new Rect (140, 40, 130, 100), "Enter the Username");
    username = GUI.TextField (new Rect (25, 60, 375, 30), username);
    GUI.Label (new Rect (140, 92, 130, 100), "Enter the Password");
    password = GUI.TextField(new Rect (25, 115, 375, 30), password);
    if (GUI.Button (new Rect (29, 160, 175, 50), "Login"))
    StartCoroutine(handleLogin(username,password));
    GUI.Label (new Rect (55, 222, 250, 100),output);
    if(GUI.Button (new Rect (225, 160, 175, 50), "Register"))
    StartCoroutine(handleRegister(username,password));
}

IEnumerator  handleLogin(string user,string pass)
{
    /*if (username == "Achu" && password == "achu123") {
                    Debug.Log (username + password);
            } else {
        Debug.Log ("error");
            }*/

    string login_url = loginCode + "?username=" + user + "&password=" + pass;
    WWW logindata = new WWW (login_url);
    yield return logindata;
    if (logindata.text == "right") {
        output="login successful";

    } else {
        output="invalid user pass";
    }


}
IEnumerator  handleRegister(string user,string pass)
{

    string register_url = registerCode + "?username=" + user + "&password=" + pass;
    WWW registerdata = new WWW (register_url);
    yield return registerdata;
        if(registerdata.text=="registered")
        {
        output="registration successful";
        }
        else{
            output="registration failed pass";
        }

    }

}
\$\endgroup\$
1
\$\begingroup\$

Your localhost is on your local development machine, you can't acces that from your android device. Try using a database which is actually hosted somewhere in the internet, it should work then.

There are lots of webhosters where you can get 2GB webspace with MySQL support for free. For example bplaced.

\$\endgroup\$
  • \$\begingroup\$ Thank u. I have a dout can you guide me hot to implement this webhosters in unity.Is there any particular webhoster for unity3d \$\endgroup\$ – user1509674 Mar 3 '16 at 8:25
  • \$\begingroup\$ I'm sorry I don't get your point. You just need to host your files and your database somewhere like pblaced or another (free) hoster and then change the url strings. There is nothing more you need to configure \$\endgroup\$ – Jonas Wirth Mar 3 '16 at 9:08
  • \$\begingroup\$ Iam not able to register in bplaced.net. Its giving an message saying " New registration disabled". Is there any other webhosters? \$\endgroup\$ – user1509674 Mar 3 '16 at 9:53
  • \$\begingroup\$ Awardspace for example ( awardspace.com/?aid=MTkyMzI3MTkxODI1MTk= ) but I've never tested that \$\endgroup\$ – Jonas Wirth Mar 3 '16 at 9:59
0
\$\begingroup\$

For a lan test, you can change "localhost" by the ip address of the machine that is running the database and then connect your android device at the same network.

Make sure your MySQL is not blocking lan access.

\$\endgroup\$
0
\$\begingroup\$

Just like guys said. You have to use your IP to connect. Use your local IP, also don't forget to create a User in the DB's that have free access, otherwise you aren't going to be able to connect in the server(because MOST DB's can't be accessed by outsider IP's of your connection

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.