Accueil Forums J’ai besoin d’aide Unity3D Connexion php Répondre à : Connexion php

#1678

Roazo
Participant

Pour vous donner une petite idée de ce que j’ai fais voici mon code :
<pre class=”lang:c# decode:true ” >using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;
using UnityEngine.UI;
using System.IO;

public class DataBaseManager : MonoBehaviour {
public string host;
public string database;
public string username;
public string password;
public Text TxtState;
MySqlConnection con;
public InputField IfLogin;
public InputField IfPassword;
public Text TxtLogin;
public string DataString;

struct _Player
{
public int ID;
public string Pseudo;
public string Password;
public int Point;
}

_Player Player;

//
public static DataBaseManager _Instance;

public DataBaseManager Script;

void Awake()
{
DontDestroyOnLoad(this);
}

void ConnectBDD () {

string constr = “Server=” + host + “;DATABASE=” + database + “;User ID=” + username + “;Password=” + password + “;Pooling=true;Charset=utf8;”;

try
{
con = new MySqlConnection(constr);
con.Open();
TxtState.text = con.State.ToString();
}
catch(IOException e)
{
TxtState.text = e.ToString();
}

}

void OnApplicationQuit()
{
Debug.Log(“fermeture Connection”);

if (con != null && con.State.ToString()!=”Closed”)
{
con.Close(); // fermer
}
}

public void Register()
{
ConnectBDD();
bool Exist = false;

MySqlCommand commandsql = new MySqlCommand(“SELECT pseudo FROM users WHERE pseudo ='” + IfLogin.text + “‘”,con);
MySqlDataReader MyReader = commandsql.ExecuteReader();

while (MyReader.Read())
{
if(MyReader[“pseudo”].ToString() != “”)
{
TxtLogin.text = “Login already exist”;
Exist = true;
}

}
MyReader.Close();
if (!Exist)
{
string command = “INSERT INTO users VALUES(default,'” + IfLogin.text + “‘,'” + IfPassword.text + “‘,”)”;
MySqlCommand cmd = new MySqlCommand(command, con);

try
{
cmd.ExecuteReader();
TxtLogin.text = “Register successfull”;
catch (IOException e)
{
TxtState.text = e.ToString();

}

cmd.Dispose();
con.Close();
}

}

public void Login(string scenename)
{
ConnectBDD();
string pass = null;
try
{

MySqlCommand commandesql = new MySqlCommand(“SELECT * FROM users WHERE pseudo ='” + IfLogin.text + “‘”, con);
MySqlDataReader Myreader = commandesql.ExecuteReader();
while (Myreader.Read())
{

pass = Myreader[“password”].ToString();
if (pass == IfPassword.text)
{
Player.ID = (int)Myreader[“ID”];
Player.Pseudo = Myreader[“pseudo”].ToString();
Player.Password = Myreader[“password”].ToString();
Player.Point = (int)Myreader[“point”];
TxtLogin.text = “Number of clics : ” + Player.Point;
DataString = Myreader[“pseudo”].ToString();
UnityEngine.SceneManagement.SceneManager.LoadScene(scenename);

}
else
{
TxtLogin.text = “Invalid pseudo or password”;
}
}

if (pass==null)
{
TxtLogin.text = “Your account does not exist”;
}
Myreader.Close();

}
catch (IOException e)
{
TxtState.text = e.ToString();
}

con.Close();
}

public void Point()
{
ConnectBDD();

Player.Point++;
string command = “UPDATE users SET point='” + Player.Point + “‘ WHERE pseudo='” + Player.Pseudo + “‘;”;
MySqlCommand cmd = new MySqlCommand(command, con);
try
{
cmd.ExecuteReader();
TxtLogin.text = “Succès”;
}
catch(IOException e)

{
TxtState.text = e.ToString();
}
cmd.Dispose();
con.Close();
}

}