dpi74187 2019-04-28 16:44
浏览 42
已采纳

有没有办法像这样在MySQL中显示数组? (“一” =>“1”)

I'm trying to save an array in a MySQL table, I serialize it and it shows something like a:3:{s:8:"One";s:1:"1";s:6:"Two";s:2:"2";... but I don't want it like this, I want something like this {One = 1, Two = 2} or something similar without those weird characters "a:4", "s:3", I was trying to look up and I was told to deserialize, but it isn't the solution i'm looking for as it shows something like {1,2}. Is there a way to make it look like I'm saying?

This is what I tried to do to deserialize:

$r9 = array("One"=>"1", "Two"=>"2", "Three"=>"3");


    $serializedArray = serialize($r9);
$decoded = unserialize($serializedArray);
    $respuestaCompleta = $cadena_equipo = implode(",", $decoded);;

    $conn = new mysqli($servername, $username, $password, $dbname);
    $sql = "INSERT INTO encuesta (id, pregunta, respuesta) VALUES ('$id', '$q9', '$respuestaCompleta')";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }


    mysqli_close($conn);

  • 写回答

1条回答 默认 最新

  • douqu2712 2019-04-28 19:19
    关注

    I think you are looking for the following:

    $r9 = array("One"=>"1", "Two"=>"2", "Three"=>"3");
    
    $serializedArray = json_encode($r9); // or use serialize() here
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    $sql = "INSERT INTO encuesta (id, pregunta, respuesta) VALUES ('".$id."', '".$q9."', '".$serializedArray."')";
    if($conn->query($sql) === true){
        echo "New record created successfully";
    }
    else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    
    mysqli_close($conn);
    

    To get the real array again use this code:

    $conn = new mysqli($servername, $username, $password, $dbname);
    $sql = "SELECT respuesta FROM encuesta;";
    $result = $conn->query($sql);
    $row = $result->fetch_array();
    
    $r9 = null;
    if(is_array($row) && count($row) > 0){
        $r9 = json_decode($row[0], true); // or unserialize() if serialize was used
    }
    

    I recommend to use json_encode(). serialize() is a php specific command. This means that other programming languages, which may interact with your database in future, cannot use the data. In addition you json gives you a more genral way. In my opinion it is easier to read. serialize() is more powerful than json_encode(). But you because you are not using objects this is no extra you need.

    In general you are converting your array to a string (with eigher the serialize() or the json_encode() method). The database cannot handle an array because there are no arrays in MYSQL. Also this is a structure of the php language.

    The generated string is equal to the array representation. MYSQL can handle strings so this string can then be saved to the database.

    If you are loading the value from the database again you will receive the saved string. This string can then be converted back to an array by using the opposite command (json_decode() or unserialize()).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系