dousi4472 2017-07-18 13:34
浏览 15
已采纳

使用php将数组保存到mysql [关闭]

i have array in swift like this one

["002012295915", "00971595502", "8885555512", "5555648583", "4085555270", "0562825196", "01147220964"]

what is the best way to save into mysql database using php code

  • 写回答

1条回答 默认 最新

  • doushai4890 2017-07-18 14:09
    关注

    If you're planning on saving all of the array as one string in the database you can just use mysqli_query, and mysqli_real_escape_string:

    Saving all of the array as one string:

    $arr = ["002012295915", "00971595502", "8885555512", "5555648583", "4085555270", "0562825196", "01147220964"];
    $con = mysqli_connect("localhost","root","pass","db");
    mysqli_query($con,"insert into TABLE values('".mysqli_real_escape_string($con,$arr)."')");
    

    And if you want to print it later just use the select from TABLE where.

    Saving each value by itself (without regex knowledge):

        //LET'S MAKE THE ARRAY MORE BEAUTIFUL FOR THE LOOP WITHOUT REGEX.
    
        $arr = str_replace('[','',$arr); 
        $arr = str_replace(']','',$arr);
        $arr = str_replace('"','',$arr);
    
    //NOW THE ARRAY LOOKS LIKE THAT: 002012295915, 00971595502, 8885555512, 5555648583, 4085555270, 0562825196, 01147220964
    //WE JUST NEED TO SPLIT IT NOW
    
    
    $new_Array = explode(',',$arr); //DEVIDE ARRAY ELEMENT BY ','
    
    //OK NOW WE JUST NEED TO PUT EACH ONE AS A SINGULAR VALUE IN THE DATABASE.
    
     $con = mysqli_connect("localhost","root","pass","db");
    
    foreach($new_Array as $data)
    {
       mysqli_query($con,"INSERT INTO TABLE VALUES('$data')");
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用