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 Coroutine

Discussion in 'Scripting' started by Keysaw, Oct 25, 2015.

  1. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    Hello !

    I'm trying to pass some data into a MySQL Database, using a PHP file on my website. To do this, I used this function :
    Code (CSharp):
    1. public IEnumerator UploadData(string id, string name)
    2.     {
    3.         url = url + "?id=" + id + "&name=" + name;
    4.         WWW www = new WWW(url);
    5.         yield return www;
    6.     }
    This function is then called inside a loop, which looks inside an array to retrieve the "id" and "name" (when I use Debug.Log inside that loop, it prints perfectly each "id" and "name" values).

    So, inside the loop, I call my function this way :
    Code (CSharp):
    1. StartCoroutine(UploadData(id, name));
    My problem is that only the first "id" and "name" of my array are passed into the database, but many times (for exemple, if I have 20 different values inside my array, I will get the first ID and NAME 20 times inside the database).

    Any ideas of what I should do?

    I hope that I am clear...

    Thanks!
     
  2. Keith73

    Keith73

    Joined:
    Oct 27, 2015
    Posts:
    1
    Hi Keysaw,

    You are only showing one side of the problem. What does your for loop like? As it does not appear to be an issue with function UploadData()

    Wait...

    byte index = 20;

    for (index = 0; index < 20; index ++)
    {
    StartCoroutine(UploadData(id[index], name[index]));
    }

    Just a suggest, based on it printing the same thing 20 times, indexing the id and name should solve this.
     
  3. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    Hello, and thank you for your help!

    But I get why it did that : I'm incrementing my url everytime the coroutine is called, so it adds '"id" and "name" to it on each call (and only the first ones are placed in the database).

    Like this :
    Code (CSharp):
    1. ...upload.php?id=myid&name=myname
    2. ...upload.php?id=myid&name=myname?id=myidd&name=mynamee
    3. ...upload.php?id=myid&name=myname?id=myidd&name=mynamee?id=myiddd&name=mynameee
    So to fix that, I just had to add
    Code (CSharp):
    1. url = null;
    as the first line in the coroutine. My url is then reset and everything works fine.

    Even if I think that your way is much cleaner than mine, it may be complicated because I'm calling this coroutine from several places in my code. Sometimes the parameters are optional, and sometimes it doesn't come from an array. So I think I will just keep going with the url resetting.

    I think I better understand the coroutine process right now, thank you very much!
     
    Last edited: Oct 28, 2015