- Home /
Problem is not reproducible or outdated
Getting Specific mySql Row by searching table with a name
Hi
I want to get specific data such as how long the user played the level and how much hp he has in the end of the level. And i want to get this info by using a name:
And through the name i want to get the whole row
Example:
Lets say i search using Max as a name. Then I whould want to get this row:
Current Code PHP / MYSQL:
<?php
$servername = "localhost";
$username = "mix";
$password = "hellogai";
$dbname ="mix";
$tablename ="sidescroller";
$name = $_POST['name'];
$recivedHash = $_POST['hash'];
// create conention to database
$db = new mysqli ($servername, $username , $password, $dbname);
//check connection
if($db->connect_error){
die("Connection Lost: " . $db->connect_error);
}
$sql = "SELECT Name, Time, Hp FROM " . $tablename . "WHERE Name = ". $name ;
//Query the database
$result = $db->query($sql);
//show if there is a result
if($result->num_rows > 0)
{
//output data of each row
$numbRow =0;
while($row = $result->fetch_assoc())
{
echo $row['time'] . "-" . $row['hp'];
}
}
$db->close();
?>
Current Unity Code:
private string secretKey = "Maxil"; //password
string UsernamesURL = "http://localhost/Highscore/Unity/usernames.php";
string NameInfoURL = "http://localhost/Highscore/Unity/nameinfo.php";
string Allnames;
public List<string> IndividualUsers;
string[] vectorUsers;
string oldname;
public Dropdown dropdownlist;
public Text CurrentName;
public Text NameInfoText;
public static string PostingName;
// Use this for initialization
void Start () {
StartCoroutine(GetNames());
oldname = " ";
PostingName = " ";
}
// Update is called once per frame
void Update()
{
if (UserCreation.looper ==1)
{
StartCoroutine(GetNames());
UserCreation.looper = 0;
}
if (oldname != PostingName)
{
StartCoroutine(Postname(PostingName)); //Postingname
oldname = PostingName;
StartCoroutine(GetNameInfo());
}
}
public void Dropdown_index(int index)
{
CurrentName.text = IndividualUsers[index];
PostingName = CurrentName.text;
}
IEnumerator Postname(string name)
{
string hash = Md5sum(name+ secretKey);
//create form with the info connected to webpage POST
WWWForm form = new WWWForm();
form.AddField("name", name);
form.AddField("hash", hash);
//connect and send the form, to POST on webpage
WWW www = new WWW(NameInfoURL, form);
yield return www; //wait intill connection complete;
if (www.error != null)
{
Debug.Log("Error: " + www.error);
}
else
{
NameInfoText.text = www.text;
}
}
IEnumerator GetNameInfo()
{
WWW nameinfo_get = new WWW(NameInfoURL);
yield return nameinfo_get;
if (nameinfo_get.error != null)
{
Debug.Log("There was an error getting the score: " + nameinfo_get.error);
}
else
{
NameInfoText.text = nameinfo_get.text;
}
}
IEnumerator GetNames()
{ //split string
WWW names_get = new WWW(UsernamesURL);
yield return names_get;
if (names_get.error != null)
{
Debug.Log("There was an error getting the score: " + names_get.error);
}
else
{
Allnames = names_get.text;
vectorUsers = Allnames.Split(',');
IndividualUsers = new List<string>(vectorUsers);
dropdownlist.ClearOptions();
dropdownlist.AddOptions(IndividualUsers);
}
}
public string Md5sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
//encrypt
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
string hashString = "";
//convert to encrypted string
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
}
I can't see any concrete question here. Your SQL query does already return that row for the given name (or any row that has that name). Too much code and no actual question asked. What's your actual problem?
Since the question is way to unspecific and not really about Unity I'm closing this question. If you know your actual question feel free to ask a new question. $$anonymous$$eep in $$anonymous$$d to only include necessary information and to describe your problem properly. For reference you could link this question in your new question.
Follow this Question
Related Questions
Using Unity to connect to a Database 3 Answers
php, sql security 3 Answers
how make player database 1 Answer
Is it possible to create GameObjects dinamically from a server? 1 Answer