Jump to content
#StayAtHome ×
J450n

Database entries to C# Question:

Recommended Posts

Is there any other way of getting PHP form data into C# any other way besides calling  

www.downloadHandler.text

I am having issues  bringing all the entries into C# and breaking them all up.  I can do one row fine but multiple rows isn't working.  I keep getting an out range error.

This is my PHP echo

echo $row['userName']. '|' .$row['level']. '|' .$row['points']. '|' .$row['killRate']. '/';

And this is my C# code

                string nothing = "Not Placed";
                string Data_string = www.downloadHandler.text;
                string[] DataArray;
                DataArray = Data_string.Split('/');

                int numberOfEntries = DataArray.Length;
                Debug.Log(numberOfEntries);

                if (DataArray[0] == null || numberOfEntries == 1)
                {
                    DataArray[0] = nothing;
                    Debug.Log("Data Array [0] isn't there");
                    High_Points_1.text = DataArray[0];

                }
                else
                {
                    High_Points_1.text = DataArray[0];
                    //Debug.Log(DataArray.Length);
                }
                if (DataArray[1] == null || numberOfEntries == 2)
                {
                    DataArray[1] = nothing;
                    Debug.Log("Data Array [1] isn't there");
                    High_Points_2.text = DataArray[1];
                }
                else
                {
                    High_Points_2.text = DataArray[1];
                    //Debug.Log(DataArray.Length);
                }
                if (DataArray[2] == null || numberOfEntries == 3)
                {
                    DataArray[2] = nothing;
                    Debug.Log("Data Array [2] isn't there");
                    High_Points_3.text = DataArray[2];
                }
                else
                {
                    High_Points_3.text = DataArray[2];
                }
                if (DataArray[3] == null || numberOfEntries == 4)
                {
                    DataArray[3] = nothing;
                    Debug.Log("Data Array [3] isn't there");
                    High_Points_4.text = DataArray[3];
                }
                else
                {
                    High_Points_4.text = DataArray[3];
                }
                if (DataArray[4] == null || numberOfEntries == 5)
                {
                    DataArray[4] = nothing;
                    Debug.Log("Data Array [4] isn't there");
                    High_Points_5.text = DataArray[4];
                }
                else
                {
                    High_Points_5.text = DataArray[4];
                }

I have two entries in the database.  But when I debugged the number int he array I get 3 strings.

I want to show the top five entries in the database.

Share this post


Link to post
Share on other sites

Very confusing.

  • You say you have 2 entries in the database.
  • Then you say you want the top five entries in the database.
  • Your php code only echoes one entry

Not surprised you're having problems.

10 minutes ago, J450n said:

Is there any other way of getting PHP form data into C#

Don't know. This is a PHP forum.

Share this post


Link to post
Share on other sites

It occurred to me that it might be easier to

  • query for the first 5 records
  • fetchAll() on the result set
  • json encode the array of results

then echo the json string.

In C#, convert the json back to an array and process.

Share this post


Link to post
Share on other sites

The database will be have more than five entries.  For testing I want to be able to say if there isn't anything there just say this.  But my code above isn't working.

The PHP code does echo the two entries just like the other issue I had you helped me with.  I understand that I only get one string from the echo and the "/" is splitting them apart so I can display them separately. 

But because there isn't a third, fourth and fifth I am getting the out of bounds error.

Can I echo each individual row in the PHP and get it into the C# with more control other than the "www.downloadhandler.text"?

 

Share this post


Link to post
Share on other sites
9 minutes ago, Barand said:

It occurred to me that it might be easier to

  • query for the first 5 records
  • fetchAll() on the result set
  • json encode the array of results

then echo the json string.

In C#, convert the json back to an array and process.

I tried that but seems like a hassle since I'm using the database and it is handling all the data nicely.  If I could just connect to the MySQL database straight with out having the app on the server that would be nice but my host doesn't allow it.

 

And with the Json would need to be split up too

Edited by J450n

Share this post


Link to post
Share on other sites
$res = $conn->query("SELECT username
                          , level
                          , points
                          , killrate
                     FROM atable
                     LIMIT 5;
                    ");
$data = $res->fetchAll();
echo json_encode($data);

That doesn't look like much of a hassle.

Share this post


Link to post
Share on other sites

I'm not up to speed enough to tackle Json.  I tried to do that route but couldn't get it to work.  In that case I wouldn't need a DB, I could just serialize it...but that's out of my comfort zone.

Share this post


Link to post
Share on other sites

Forget the json then.

This should give you the first 5 records in the format your C# is currently expecting

$res = $conn->query("SELECT username
                          , level
                          , points
                          , killrate
                     FROM atable
                     ORDER BY points DESC
                     LIMIT 5;
                    ");
$data = [];
foreach ($res as $row) {
    $data[] = join('|', $row);
}
echo join('/', $data);

 

Share this post


Link to post
Share on other sites

I need to learn this.

I built the app and those out of bounds errors are not crashing the app or anything.  Is it best practice to not worry about those errors because they will resolve themselves anyway?

Thank you.  getting into it now.

Share this post


Link to post
Share on other sites
4 hours ago, Barand said:

Forget the json then.

This should give you the first 5 records in the format your C# is currently expecting


$res = $conn->query("SELECT username
                          , level
                          , points
                          , killrate
                     FROM atable
                     ORDER BY points DESC
                     LIMIT 5;
                    ");
$data = [];
foreach ($res as $row) {
    $data[] = join('|', $row);
}
echo join('/', $data);

 

@Barand  So I get this now.  Thank you.  It is similar in C#, but I didn't realize I could do this in PHP.

Insert other media

 

Share this post


Link to post
Share on other sites

Now I see how I can split up the individual columns for the rows and to break them up instead of having a string of the row.

Thanks again

Edited by J450n

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.