duanluanlang8501 2016-04-13 10:12
浏览 51
已采纳

将PHP数组存储在MySQL中

So i'm still learning PHP, MySQL and a few other languages. While i'm trying to improve and learn i have develpoed a browser game.

Now i'm trying to save a "battle log" from each fight that takes place, that will allow my users to go back and look at their past fights.

To solve this i have tried to store each fight round into a php array. The problem for me now is to save that array into my database, for a user to request and have displayed to them.

I have tried to mess around with PHP's serialize and also with json_encode. But i fail to get them stored in my DB as it will eighter give me a syntax error or a empty row.

Here a example to display what i am trying to achive!

$i = 1;
$battle_log = array();

while( $i <= 10)
{
     $battle_log[] = "Round ".$i."<br>";
     $i++;
}

foreach ($battle_log as $rounds => $round) {
    echo "".$round."";
}

$save_log = json_encode($battle_log);
$sql = "INSERT INTO battle_log_test VALUES fighter1_id='13', fighter2_id='45', battle_log='".$save_log."' ";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

All input to help me solve this problem will be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • douze2890241475 2016-04-13 10:19
    关注

    You will have to use:

    $query = "INSERT INTO battle_log (fighter1_id,fighter2_id,battle_log) VALUES (13,45,'".$save_log."');";
    $sql = $mysqli_query($connection,$query) or die($mysqli_error($connection));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教