- Home /
How do i search a mysql database in unity 5
Hi All,
how do i search for a Row using mysql using a public string im new to mysql and php
---------------- php script-----------------------------
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "online_pw_build";
$CurrentUser = "currentuser";
$conn = new mysqli($servername, $username, $password, $dbName);
if(!$conn) {
die("connection failed". mysqli_connect_error());
}
$sql = "SELECT Onwer FROM db_toons";
$check = mysql_query("SELECT * FROM `db_toons` WHERE `Onwer` LIKE 'currentuser'");
{
die ("Username does not exist \n");
}
{
$CurrentUser = (currentuser);
while($row = mysql_fetch_assoc($check))
{
if (currentuser == $row['Onwer'])
echo "ID:".$row['ID'] . "|Onwer:".$row['Onwer']. "|Toonone:".$row['Toonone']. ";";
}
}
?>
-------------C# script----------------
using UnityEngine;
using System.Collections;
public class CurToon : MonoBehaviour {
public string currentuser;
IEnumerator Start(){
WWW CurToon = new WWW("http://localhost/online_pw_build/CurToon.php");
yield return CurToon;
string CurToonrString = CurToon.text;
print (CurToonrString);
}
what i want to happen a user login to a database ID Onwer toonone toontwo this part is done
after they login i want to search for the Onwer they logged in with then return that row back to unity
the way i did it was by echoing out all the information within the PHP file and putting a certain key between each one. For example CurToon.text would be equal to "usernameExample:passwordExample:idExample", then I split the string up and added each split string into an array. so I know infoArray[0] is the username, [1] is the password, etc.. let me know if you need a coded example.
Answer by thornekey · Oct 18, 2015 at 10:49 AM
ok so instead of doing echo use the die funciton. this will stop the page at this message. also use the trim function as sometimes you get a whitespace at the beginning. if not just remove it. Putting the '--' acts a form of key for us to know when to split the string. You can use anything!
die(ltrim($row["ID"] . "--" . $row["Owner"] . "--" . $row["Toonone"]));
Next, in your script in unity change your IEnumerator to this:
void Start() {
WWWForm form = new WWWForm();
// add as many of these as you need. you will use the $_POST variable funciton in your php file.
// so in your PHP file wyou will have $owner = $_POST['the field name']; and it will equal whatever the "variable name" 's value is.
form.AddField("the field name", variable name);
string link = "link to your PHP file";
Debug.Log(link);
WWW curToon = new WWW(link, form);
StartCoroutine(GetInfo(curToon ));
}
}
IEnumerator GetInfo(WWW curToon) {
string infoString;
yield return curToon;
infoString = curToonw.text.ToString().TrimStart();
if (infoString == "0") {
Debug.Log ("no player info..");
} else {
Debug.Log(infoString);
string[] infoArray = infoString.Split(new[] { "--" }, System.StringSplitOptions.None);
string playerID = infoArray [0];
string playerName = infoArray [1];
string playerToonone = infoArray [2];
Debug.Log ("id: " + playerID);
Debug.Log ("owner: " + playerName);
Debug.Log ("Toonone: " + playerToonone );
}
}
There is probably an easier way to do it. I did it this way though and it works just fine.
:) yes it does work
but its returning the first line of the table and not the currentuser
What do you mean the first line? Show me your output.
@pauld1988 try if ($CurrentUser == $row['Onwer']) {
on line 29 on your PHP file.
Undefined variable: Currentuser in C:\xampp\htdocs\online_pw_build\CurToon.php on line 29
@pauld1988 sorry, your original code said $CurrentUser not $Currentuser. Variables are case sensitive.
Your answer
Follow this Question
Related Questions
Unity PHP login always returning true 3 Answers
My code to php -> sql in PC good, in Android not work. plese fast 1 Answer
Is it possible to send a table name through a wwwform and use it in a php query? 0 Answers
Is it possible to send and recieve data from/to a MySql server? 4 Answers
unity3d connect to php or coldfusion for database information 2 Answers