Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

MySql and C# Communication

Discussion in 'Scripting' started by Connor_13_, Jan 24, 2015.

  1. Connor_13_

    Connor_13_

    Joined:
    Mar 14, 2014
    Posts:
    40
    So I've looked at a few tutorials about communicating between MySql and C# scripts in Unity. When I run the Coroutine, the console tells me that the upload was completed successfully; however, when I run SELECT * FROM StudentUsers in my terminal, it says empty set. Now, I know I have an error with something somewhere but i don't know where... Thanks for the help.

    My PHP Script:
    Code (CSharp):
    1. <?php
    2.         $db = mysql_connect('localhost', '****', '****') or die('Could not connect: ' . mysql_error());
    3.         mysql_select_db('MFHS_FanApp_db') or die('Could not select database');
    4.         // Strings must be escaped to prevent SQL injection attack.
    5.         $first_name = mysql_real_escape_string($_GET['first_name'], $db);
    6.         $last_name = mysql_real_escape_string($_GET['last_name'], $db);
    7.         $username = mysql_real_escape_string($_GET['username'], $db);
    8.         $email = mysql_real_escape_string($_GET['email'], $db);
    9.         $password = mysql_real_escape_string($_GET['password'], $db);
    10.         $grad_year = mysql_real_escape_string($_GET['grad_year'], $db);
    11.         $points = mysql_real_escape_string($_GET['points'], $db);
    12.         $hash = $_GET['hash'];
    13.         $secretKey="mySecretKey"; # Change this value to match the value stored in the client javascript below
    14.  
    15.         $real_hash = md5($first_name . $last_name . $username . $email . $password . $grad_year . $points . $secretKey);
    16.         if($real_hash == $hash) {
    17.             // Send variables for the MySQL database class.
    18.             $query = "insert into StudentUsers values (NULL, '$first_name', '$last_name', '$username', '$email', '$password', '$grad_year', '$points');";
    19.             $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    20.         }
    21. ?>


    My C# Method:
    Code (CSharp):
    1.     IEnumerator AddStudentUser(string first_name, string last_name, string username, string email, string password, int grad_year, int points)
    2.     {
    3.         print ("Begin AddStudentUser");
    4.         //This connects to a server side php script that will add the name and score to a MySQL DB.
    5.         // Supply it with a string representing the players name and the players score.
    6.         string hash = Md5Sum(first_name + last_name + username + email + password + grad_year + points + secretKey);
    7.        
    8.         string post_url = URL_addStudentUser + "first_name=" + WWW.EscapeURL(first_name) + "&last_name=" + WWW.EscapeURL(last_name) + "&username=" + WWW.EscapeURL(username) +
    9.                             "&email=" + WWW.EscapeURL(username) + "&password=" + WWW.EscapeURL(username) + "&grad_year" + grad_year + "&points=" + points + "&hash=" + hash;
    10.         print (post_url);
    11.         // Post the URL to the site and create a download object to get the result.
    12.         WWW hs_post = new WWW(post_url);
    13.         yield return hs_post; // Wait until the download is done
    14.        
    15.         if (hs_post.error != null)
    16.         {
    17.             print("There was an error posting the high score: " + hs_post.error);
    18.         } else {
    19.             print ("Successfully uploaded");
    20.         }
    21.     }
     
  2. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    You're sending the email and the password as the username.
     
  3. Connor_13_

    Connor_13_

    Joined:
    Mar 14, 2014
    Posts:
    40
    I feel stupid now haha. That would be why the hashes aren't matching. Thank you!