C# 使用离线mySQL数据库在Unity for android中制作游戏
我正在做一个琐事,在最后有5个输入字段的用户数据,如姓名,姓氏,电话,文件号码和电子邮件。并使用mySQL和phpmyadmin创建了一个数据库来存储这些数据。当我为windows构建时,一切正常,但我必须为android平板电脑构建,但我无法使数据库正常工作,如果有人能帮助我,我将非常高兴 这是我的数据库管理C#unity脚本:C# 使用离线mySQL数据库在Unity for android中制作游戏,c#,php,android,database,unity3d,C#,Php,Android,Database,Unity3d,我正在做一个琐事,在最后有5个输入字段的用户数据,如姓名,姓氏,电话,文件号码和电子邮件。并使用mySQL和phpmyadmin创建了一个数据库来存储这些数据。当我为windows构建时,一切正常,但我必须为android平板电脑构建,但我无法使数据库正常工作,如果有人能帮助我,我将非常高兴 这是我的数据库管理C#unity脚本: using System.Collections; using UnityEngine.UI; using UnityEngine; public class Ge
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");
}
}
}
这是我的php登录文件:
<?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 * FROM usuarios WHERE nombre LIKE '$nom' AND apellido
LIKE '$ape' AND ci LIKE '$ci' AND tel LIKE '$tel' AND mail LIKE '$mai'";
$resultado = mysqli_query($conexion, $sql);
if (mysqli_num_rows($resultado)>0)
{
echo "bien";
}
else
{
echo "mal";
}
}
?>
和我的注册php文件:
<?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 * FROM usuarios WHERE ci LIKE '$ci'";
$resultado = mysqli_query($conexion, $sql);
if (mysqli_num_rows($resultado)>0)
{
echo "402";
}
else
{
$sql = "INSERT INTO usuarios (id, nombre, apellido, ci, tel, mail)
VALUES (NULL, '$nom', '$ape', '$ci', '$tel', '$mai')";
$resultado = mysqli_query($conexion, $sql);
echo "201";
}
}
?>
Localhost无法在android中工作,因为它没有像您的pc那样设置本地服务器。如果您想存储在本地数据库中,您需要使用类似sqlite()的东西。sqlite的assetstore中还有免费资产:
对于远程服务器,使用实际的服务器凭据将使其工作。我忘了添加数据库必须作为本地主机脱机。那么,问题是否仍然存在?游戏中没有错误,但是数据库没有存储信息,也找不到问题是的,问题是我无法将表127中的数据存储在数据库中。0.0.1是本地地址(与游戏运行的机器相同)。如果您在平板电脑上玩游戏并尝试将数据发送到127.0.0.1,则您正在尝试将数据发送到该平板电脑。我猜你的平板电脑上没有web服务器和mysql服务器?
<?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 * FROM usuarios WHERE ci LIKE '$ci'";
$resultado = mysqli_query($conexion, $sql);
if (mysqli_num_rows($resultado)>0)
{
echo "402";
}
else
{
$sql = "INSERT INTO usuarios (id, nombre, apellido, ci, tel, mail)
VALUES (NULL, '$nom', '$ape', '$ci', '$tel', '$mai')";
$resultado = mysqli_query($conexion, $sql);
echo "201";
}
}
?>