Jump to content
#FlattenTheCurve ×
J450n

Database rsults formatting:

Recommended Posts

I am trying to get my database entry's to C# in Unity to display them.

This formatting works

echo ("|".$row['GUID']. PHP_EOL. "|".$row['userName']. PHP_EOL. "|".$row['health']. PHP_EOL. "|".$row['level']. PHP_EOL. "|".$row['points']. PHP_EOL. "|".$row['killBonus']. PHP_EOL. "|".$row['killRate']. PHP_EOL. "|".$row['kills']. PHP_EOL. "|".$row['misses']. PHP_EOL. ";");

But When I get it into Unity using this code

string Data_string = www.downloadHandler.text;        
                string[] DataArray;
                DataArray = Data_string.Split('|');
                //Debug.Log(DataArray);

                string guid = (DataArray[0]);
                Debug.Log(guid);
                string username = (DataArray[1]);
                Debug.Log(username);
                int health = System.Convert.ToInt32(DataArray[2]);
                Debug.Log(health);
                int level = System.Convert.ToInt32(DataArray[3]);
                Debug.Log(level);
                int points = System.Convert.ToInt32(DataArray[4]);
                Debug.Log(points);
                int killBonus = System.Convert.ToInt32(DataArray[5]);
                Debug.Log(killBonus);
                float killRate = float.Parse(DataArray[6]);
                Debug.Log(killRate);
                float kills = float.Parse(DataArray[7]);
                Debug.Log(kills);
                float misses = float.Parse(DataArray[8]);
                Debug.Log(misses);

It puts out the first string "GUID" but then throws an error before "username".

Can someone help me understand what I am doing wrong?

 

 

This is the error I am getting.

 

FormatException: Input string was not in a correct format.
System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0)
System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0)
System.Int32.Parse (System.String s, System.IFormatProvider provider) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0)
System.Convert.ToInt32 (System.String value) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0)
Login+<LoginCalled>d__4.MoveNext () (at Assets/CODE/Login.cs:52)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

 

Edited by J450n

Share this post


Link to post
Share on other sites

If you are splitting on "|", why put the PHP_EOLs in there?

Also, try omitting the leading "|" in your string, so you have

guid|username|health|...|misses

When you split that on "|" the guid should be item 0.

With the leading "|" (i.e "| guid | username | …") I would expect item 0 to be blank after the split and guid would be item 1..

Share this post


Link to post
Share on other sites

After posting this I tried this;

 

echo (" ".$row['GUID']. " ".$row['userName']. " ".$row['health']. "  ".$row['level']. " ".$row['points']. " ".$row['killBonus']. " ".$row['killRate']. " ".$row['kills']. " ".$row['misses']. ";");

and I got the same result.  So do I treat the " " to be an item as well when placing the [number of item] in the C# code?

Share this post


Link to post
Share on other sites

You have taken the "|" characters out! How do expect to split it?

The echoed string should look like

guid|username|health|...|misses

I.E.

$row = [ 'guid' => 'xyz',
         'username' => 'fred',
         'health' => 1, 
         'level' => 42
       ];
       
echo $row['guid'] . '|' . $row['username'] . '|' . $row['health'] . '|' . $row['level'];

or (easier if you want every $row item in their natural ORDER)

echo join('|', $row);

Then when you split it, the array will look like

Array
(
    [0] => xyz
    [1] => fred
    [2] => 1
    [3] => 42
)

 

Share this post


Link to post
Share on other sites

I changed the echo to this

 

echo $row['GUID'] . '|' . $row['userName']. '|' .$row['health']. '|' .$row['level']. '|' .$row['points']. '|' .$row['killBonus']. '|' .$row['killRate']. '|' .$row['kills']. '|' .$row['misses'];

 

And it works perfectly.  I knew there was some formatting of the echo that was screwing me up.  

Thank you very much for your help.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.