So I'm making a raffle game so when you press B you get a random name with all the information of the winner. I get the data from a local SQL server and I want to make a list of the winners by saving the string data.

Here is my PHP script to get data:

<?php

$servidor  = 'localhost';
$user      = 'root';
$password  = '';
$baseDatos = 'trivia';

$conexion = new mysqli($servidor, $user, $password, $baseDatos);
if (!$conexion)
{
echo "400";
}
else
{
$sql  = "SELECT * FROM usuarios ORDER BY RAND() LIMIT 1";
$resultado = mysqli_query($conexion, $sql);
if (mysqli_num_rows($resultado)>0)
{
  while ($row = mysqli_fetch_assoc($resultado)){
    echo "id:".$row['id']."@" ."factura:".$row['factura']."~" . 
"ruc:".$row['ruc']. "`" . "nombre:".$row['nombre']. "!";
}
}   
}

?>

And this is my c# script to display the data in unity:

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class DataLoader : MonoBehaviour
{
public Text id;
public string ID = "id:";

public Text factura;
public string NFA = "factura:";

public Text ruc;
public string NRU = "ruc:";

public Text nombre;
public string NOM = "nombre:";

public string[] items;


void Update()
{
if (Input.GetKeyDown(KeyCode.B))
    StartCoroutine("Start");
}

IEnumerator Start()
{
WWW coneccion = new WWW("http://localhost/trivia/getdatos.php");
yield return (coneccion);
if (Input.GetKey(KeyCode.B))
{
    WWW getdatos = new WWW("http://localhost/trivia/getdatos.php");
    yield return getdatos;

    string getdatosString = getdatos.text;

    print(getdatosString);
    items = getdatosString.Split('@');
    print(GetDataValue(items[0], "id:"));
    id.text = "id: " + GetDataValue(items[0], "id:");

    print(getdatosString);
    items = getdatosString.Split('~');
    print(GetDataValue(items[0], "factura:"));
    factura.text = "factura: " + GetDataValue(items[0], "factura:");

    print(getdatosString);
    items = getdatosString.Split('`');
    print(GetDataValue(items[0], "ruc:"));
    ruc.text = "ruc: " + GetDataValue(items[0], "ruc:");

    print(getdatosString);
    items = getdatosString.Split('!');
    print(GetDataValue(items[0], "nombre:"));
    nombre.text = "nombre: " + GetDataValue(items[0], "nombre:");
}


}

string GetDataValue(string data, string index)
{
string value = data.Substring(data.IndexOf(index) +index.Length);
if(value.Contains("~"))value = value.Remove(value.IndexOf("~"));
return value;

} 

}

Then i thought why not use the same database to create a new table, so I did that and so far I can save the names but only in the editor as a public string but I can't get the data from the other script. This is my Data inserter script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DataInserter : MonoBehaviour
{

string CreateGanadoresURL = "http://localhost/trivia/ganadores.php";

public string inputFactura;
public string inputRuc;
public string inputNombre;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space)) CreateGanadores(inputFactura, 
inputRuc, inputNombre);
}

public void CreateGanadores(string factura, string ruc, string nombre)
{
WWWForm form = new WWWForm();
form.AddField("facturaPost", factura);
form.AddField("rucPost", ruc);
form.AddField("nombrePost", nombre);

WWW www = new WWW(CreateGanadoresURL, form);
}
}

And this is the datainserter php:

<?php

 $servidor  = 'localhost';
 $user      = 'root';
 $password  = '';
 $baseDatos = 'trivia';

$factura = $_POST["facturaPost"]; //"123456789";
$ruc = $_POST["rucPost"]; //"1958683-3";
$nombre = $_POST["nombrePost"]; //"veronica";

$conexion = new mysqli($servidor, $user, $password, $baseDatos);
if (!$conexion)
{
echo "400";
}
else
{
$sql  = "INSERT INTO ganadores (factura, ruc, nombre)
         VALUES ('".$factura."','".$ruc."','".$nombre."')";
$resultado = mysqli_query($conexion, $sql);

if (!$resultado) echo "there was an error";
else echo "Everything ok.";
}   

?>

Any suggestion or help or a different approach would be nice!

Thanks in advance.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.