c# - Making a Game in Unity for android with an offline mySQL database -
im making trivia , @ end has 5 inputfields user data such name, last name, phone, document number , email. , did database mysql , phpmyadmin store data. when build windows works fine have build android tablet, cant manage data base working, glad if can me!
this c# unity script database management:
using system.collections; using unityengine.ui; using unityengine; public class gestorbd : monobehaviour { public inputfield txtnombre; public inputfield txtapellido; public inputfield txtci; public inputfield txttel; public inputfield txtmail; public string nombrenombre; public string apellidoapellido; public int cici; public int tel; public string mail; public bool sesioniniciada = false; /// respuestas web /// /// 200 = datos encontrados /// 201 = usuario registrado /// /// 400 = no pudo establecer coneccion /// 401 = no enconto datos /// 402 = el usuario ya existe public void iniciarsesion() { startcoroutine(login()); startcoroutine(datos()); } public void registrarnombre() { startcoroutine(registrar()); } ienumerator login() { www coneccion = new www("http://127.0.0.1/trivia/login.php?nom=" + txtnombre.text + "&ape=" + txtapellido.text + "&ci=" + txtci.text + "&tel=" + txttel.text + "&mai=" + txtmail); yield return (coneccion); if (coneccion.text == "200") { print("el usuario si existe"); } else if (coneccion.text == "401") { print("usuario o contrasena incorrectos"); } else { print("error en la coneccion con la base de datos"); } } ienumerator datos() { www coneccion = new www("http://127.0.0.1/trivia/datos.php?nom=" + txtnombre.text + "&ape=" + txtapellido.text + "&ci=" + txtci.text + "&tel=" + txttel.text + "&mai=" + txtmail); yield return (coneccion); if (coneccion.text == "401") { print("usuario o contrasena incorrectos"); } else { string[] ndatos = coneccion.text.split('^'); if (ndatos.length != 2) { print("error en la coneccion"); } else { nombrenombre = ndatos[0]; apellidoapellido = ndatos[1]; cici = int.parse(ndatos[2]); tel = int.parse(ndatos[3]); mail = ndatos[4]; sesioniniciada = true; } } } ienumerator registrar() { www coneccion = new www("http://127.0.0.1/trivia/registro.php?nom=" + txtnombre.text + "&ape=" + txtapellido.text + "&ci=" + txtci.text + "&tel=" + txttel.text + "&mai=" + txtmail.text); yield return (coneccion); if (coneccion.text == "402") debug.logerror("usuario ya existe!"); else if (coneccion.text == "201") { nombrenombre = txtnombre.text; apellidoapellido = txtapellido.text; sesioniniciada = true; } else { debug.logerror("error en la coneccion con la base de datos"); } } } and php file login:
<?php $servidor = 'localhost'; $user = 'root'; $password = ''; $basedatos = 'trivia'; $conexion = new mysqli($servidor, $user, $password, $basedatos); $nom = $_get['nom']; $ape = $_get['ape']; $ci = $_get['ci']; $tel = $_get['tel']; $mai = $_get['mai']; if (!$conexion) { echo "error"; } else { $sql = "select * usuarios nombre '$nom' , apellido '$ape' , ci '$ci' , tel '$tel' , mail '$mai'"; $resultado = mysqli_query($conexion, $sql); if (mysqli_num_rows($resultado)>0) { echo "bien"; } else { echo "mal"; } } ?> my php file data:
<?php $servidor = 'localhost'; $user = 'root'; $password = ''; $basedatos = 'trivia'; $conexion = new mysqli($servidor, $user, $password, $basedatos); $nom = $_get['nom']; if (!$conexion) { echo "400"; } else { $sql = "select * usuarios nombre '$nom'"; $resultado = mysqli_query($conexion, $sql); if (mysqli_num_rows($resultado)>0) { while ($row = mysqli_fetch_assoc($resultado)){ echo $row['nombre']."^".$row['apellido']."^".$row['ci']."^".$row['tel'] ."^".$row['mail']; } } else { echo "401"; } } ?> and php file register:
<?php $servidor = 'localhost'; $user = 'root'; $password = ''; $basedatos = 'trivia'; $conexion = new mysqli($servidor, $user, $password, $basedatos); $nom = $_get['nom']; $ape = $_get['ape']; $ci = $_get['ci']; $tel = $_get['tel']; $mai = $_get['mai']; if (!$conexion) { echo "400"; } else { $sql = "select * usuarios ci '$ci'"; $resultado = mysqli_query($conexion, $sql); if (mysqli_num_rows($resultado)>0) { echo "402"; } else { $sql = "insert usuarios (id, nombre, apellido, ci, tel, mail) values (null, '$nom', '$ape', '$ci', '$tel', '$mai')"; $resultado = mysqli_query($conexion, $sql); echo "201"; } } ?> thanks!
localhost not work in android there's no local server set in pc. if want store in local database, you'll need use sqlite(https://www.sqlite.org). there's free asset in assetstore sqlite: https://www.assetstore.unity3d.com/en/#!/content/20660
for remote server, using actual server credentials make work.
Comments
Post a Comment