Search Unity

www read to variable

Discussion in 'Scripting' started by ratxrat, Feb 14, 2017.

  1. ratxrat

    ratxrat

    Joined:
    Nov 7, 2016
    Posts:
    19
    hai hai :)

    im have trouble about insert data from php to variable in c#, i create php and its work perfect then i can read all echo from php, btw this is my c# code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class item : MonoBehaviour {
    5.  
    6.     public string[] items;
    7.  
    8.     // Use this for initialization
    9.     IEnumerator Start ()
    10.     {
    11.         WWW itemData = new WWW ("http://localhost/mmo/item.php");
    12.  
    13.        
    14.  
    15.         yield return itemData;
    16.  
    17.        
    18.  
    19.         string itemString = itemData.text;
    20.         print (itemString);
    21.        
    22.  
    23.  
    24.     }
    25.    
    26.  
    27.    
    28.  
    29. }
    30.  
    i not sure about insert every data in my database to variable and call.

    for example
    in my msql database i have 3 coloum : name, password, email
    and get to data to c# use WWW .
    then show like this in console:
    Code (CSharp):
    1. name:test|password:1234|email:email@email.com|;name:test2|password:1234|email:email2@email.com|;name:test3|password:1234|email:email3@email.com|;
    my big question is how call data by name.
    example i call name test3 and show password , email.


    btw sory for my bad english.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Parsing data like this is a bit of a pain, and the slightest change to the format will break your code, requiring a rewrite. Plus, you'll probably have to write a new parser function for every. single. type of data you will ever receive. Therefore, if possible, the first thing I would do would be to make the server give the data in a more versatile format, like JSON; then, you can easily use JsonUtility to read it in. If your server were to spit out JSON instead of the format you have currently, all of the code below will be replaced by about two lines of much more readable code. I don't know what your server code looks like (indeed, I don't know much PHP myself), but my guess is that there is a handy PHP library that can spit out JSON from a database easily, too.

    If you don't have enough control over the server for that, you can use string.Split to parse your input. Something like this should get you started.
    Code (csharp):
    1. string searchFor = "password";
    2. string[] rows = itemString.Split(";"[0]);
    3. for (int r=0;r<rows;r++) {
    4. string[] cells = rows[r].Split("|"[0]);
    5. for (int c=0;c<cells.Length;c++) {
    6. string[] parts = cells[c].Split(":"[0]);
    7. string key = parts[0];
    8. string value = parts[1];
    9. if (key == searchFor) {
    10. Debug.Log("The password on row "+r+" is "+value);
    11. }
    12. }
    13. }
     
  3. ratxrat

    ratxrat

    Joined:
    Nov 7, 2016
    Posts:
    19
    thanks for idea about pharsing data i will try :)