所以我在c#中有一个列表,该列表正在转换为字符串,然后在PHP中转换为二维数组,然后将其发送到MySQL数据库。现在,我要如何扭转这一状况,即可以从数据库下载到PHP,而不是c#列表。
到目前为止,这是我要的内容,但是我不确定是否需要将数据下载回PHP中的数组,还是应该将其作为字符串下载并分解为c#中的列表? / p>
这是我的C#代码:
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;
这是我的php:
$shiparraytobesplit = $_POST["shipinventory"];
$ships = explode("zzz",$shiparraytobesplit);
unset($ships[count($ships)-1]);
$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));
}
将我的c#列表上传到数据库中的效果很好,但是我不确定从数据库中将其下载回c#列表的最佳方法是什么。任何建议都很棒!谢谢