- Home /
Login from database in unity
I need to connect to my database in unity.I have created a table.Then i have written a PHP code to retrive data from the database and have put that in the wamp www folder
``
Then I have called the url in the c# to connect to the database.Here is my c# code
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"))
handleLogin ();
GUI.Label (new Rect (55, 222, 250, 100),output);
/*if(GUI.Button (new Rect (225, 160, 175, 50), "Register"));
Debug.Log ("Clicked Registered button");*/
}
public void handleLogin()
{
Debug.Log ("login");
if (username == "Achu" && password == "achu123") {
Debug.Log (username + password);
} else {
Debug.Log ("error");
}
string login_url = loginCode + "username=" + username + "password=" +password;
}
Here I have done a sample for checking whether the username and password is correct for the given string.Inorder to connect to the database i have got the code in javascript.I need it in c# form.Can anybody help me plz.Here is the javacode
function checkUser() {
console = "Checking Username and Password";
var getuser_url = getuserUrl + "?username=" + usernameText + "&password=" + md5.Md5Sum(passwordText);
var cu_get = WWW(getuser_url);
yield cu_get;
if(cu_get.error) {
console = "There was an error checking your username and password: " + cu_get.error;
}
else
{
if(cu_get.data == "right")
{
console = "Welcome back " + usernameText + " hope you enjoy the game!";
yield WaitForSeconds(3.0);
userOK=true;
}
else
{
userOK = false;
console = cu_get.data;
}
}
Comment
Your answer
Follow this Question
Related Questions
Not allowed to connect to my database. 1 Answer
MySQL Funktionen Hilfe 1 Answer
Downloading data from MySql database not working on Android. 1 Answer
WWWForm Question 2 Answers
MySql C# compiler error 0 Answers