Mysql/PHP Grab specific data
I am trying to get a text field and a button for a player to enter in his/her username and then unity sends a get request to a .php and then sends unity the specific player data. In this case I am trying to get player's coordinates. So the player enters their username and it sends back a vector3 data.
Here is my PHP script. Name is the username, age is X, age2 is Y, age3 is Z.
<?php
$con = new mysqli("localhost","root","","game");
if(isset($_POST['name']) && isset($_POST['age']) && isset($_POST['age2']) && isset($_POST['age3'])){
$name = $_POST['name'];
$age = $_POST['age'];
$age2 = $_POST['age2'];
$age3 = $_POST['age3'];
$query = $con->query("SELECT * FROM items WHERE name='$name'");
if($query->num_rows > 0){
$con->query("UPDATE items SET age='$age' , age2='$age2', age3='$age3' WHERE name='$name'");
}
else{
$con->query("INSERT INTO items (name,age,age2,age3) VALUES ('$name', '$age', '$age2', '$age3')");
}
}
$result = $con->query("SELECT * FROM items");
if($result->num_rows > 0){
echo(";");
while($row = $result->fetch_assoc()){
echo("Name:".$row["name"] . "|X:" . $row["age"] . "|Y:" . $row["age2"] . "|Z:" . $row["age3"] . ";");
}
}
else{
echo("Database is empty");
}
?>
I don't have anything great for a working C# script but I have this which separates the data but that doesn't help me if I need only a specific player's data. using UnityEngine; using System.Collections;
public class Location : MonoBehaviour {
private string textFieldString = "Username";
void OnGUI ()
{
textFieldString = GUI.TextField(new Rect(500, 25, 100, 30), textFieldString, 25);
GUI.Label(new Rect(505, 75, 100, 100), textFieldString);
}
public string[] items;
// Use this for initialization
IEnumerator Start () {
WWW itemsData = new WWW("localhost/game/plrdata.php");
yield return itemsData;
string itemsDataString = itemsData.text;
print(itemsDataString);
items = itemsDataString.Split(';');
}
}
What exactly is your question/problem? It seems you already receive the correct string with the items data. So you just don't know how to parse the string?
I did not receive the one player's data that I wanted, I received all of my data from the mysql server. I have multiple player's vector3 data in a mysql server but I want to have a text box where it will search for that one player's vector3 data and only that player's data.
(Answering here because of stupid forum bug)
What line would I put " $result = $con->query("SELECT * FRO$$anonymous$$ items");" in?
I'm talking about line 20 in above PHP script. I think the filter "WHERE name='$name'" is missing and that's why it returns all items.
Compare line 20 and line 12 in the PHP script.
I know, I want the php script to show all users. I just want unity to be able to pick out a single user from that list. Thus making it so multiple players can use this at once.
So the string parsing is indeed the problem?
I would suggest to use JSON for the data transfer. It'll save you a lot of time in the long run.
http://www.kodingmadesimple.com/2015/01/convert-mysql-to-json-using-php.html
Your answer
