Mysqli to php to c#

So I have a list in c# that's being converted to a string, which is then converted to a 2-dimensional array in PHP, which is then sent to MySQL database. Now how do I go about reversing this, whereby I can download from the database to PHP than to c# list.

This is what I have so far, but I'm not sure if I need to download the data back to an array in PHP or if I should download it as a string and break it up into a list in c#?

This is my c# code:

public List<Ship> shipList = new List<Ship>();
string shipListString = "";
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("shipcount", ShipInventory.Count);  
for (int i = 0; i < shipList.Count; i++)
    shipListString = shipListString + " " + shipList[i].id + " '" + shipList[i].username + "' '" + shipList[i].name + "' " + shipList[i].power +
             "zzz";// + ShipInventory[i];            
form.AddField("shipinventory", shipListString);
WWW www = new WWW("x.x.x.x/savedata.php", form);
yield return www;

And this is my php:

$shiparraytobesplit = $_POST["shipinventory"];
$ships =  explode("zzz", $shiparraytobesplit);

$shipinfo = array_map(function($ship) {
    $tempshipinfo = explode(" ", $ship);
    $ship_assoc = [
        "id" => $tempshipinfo[1],
        "name" => $tempshipinfo[2],
        "username" => $tempshipinfo[3],
        "hp" => $tempshipinfo[4]
    return $ship_assoc;
}, $ships);

$sql = sprintf('INSERT INTO shipinventory (shipid,shipname,username,shippower) VALUES (%s)', implode(',',array_values($shipinfo[$i])));

if(!mysqli_query($con, $sql))
    echo("error description: " . mysqli_error($con));       

This is working well to upload my c# list into the database, but I'm not sure what's the best way to download it from the database back to a c# list. Any advice would be awesome! Thanks

1 answer

  • answered 2019-11-14 01:00 Hathakas

    Ok so I managed to make it work but I imagine there are better methods so please share! Thanks:

    C# Script:

    WWW www = new WWW("x.x.x.x/loaddata.php");
            yield return www;
            stringFromPHP = www.text;
            char[] delimiterENTER = new char[] {'\n' };
            char[] delimiterSPACE = new char[] { ' ' };
            shipStringArray = stringFromPHP.Split(delimiterENTER, StringSplitOptions.RemoveEmptyEntries);        
            for (int i = 0; i < shipStringArray.Count(); i++)
                string[] shipInfo = shipStringArray[i].Split(delimiterSPACE);                    
                shipList.Add(new Ship(Int32.Parse(shipInfo[0]), shipInfo[1], shipInfo[2], Int32.Parse(shipInfo[3]), shipStringArray[i]));            

    and php:

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
    $query = "SELECT shipid, username, shipname, shippower FROM shipinventory";
    if ($result = mysqli_query($con, $query)) {
        while ($row = mysqli_fetch_assoc($result)) {
          $stringtoexplodeincsharp  printf ("%s %s %s %s\n",(int) $row["shipid"], $row["username"], $row["shipname"],(int) $row["shippower"]);