【问题标题】:Save array to DB将数组保存到数据库
【发布时间】:2016-10-08 10:53:04
【问题描述】:

我是 PHP 数组的新手...但我创建了一个数组来获取已在表单中提交的 2 个日期之间的所有日期。

见下文:

$endDate=$_POST[todate];
            $startDate =$_POST[fromdate];

print_r(getDatesFromRange( "$datef", "$dateto" ));

function getDatesFromRange($startDate, $endDate)
{
    $return = array($startDate);
    $start = $startDate;
    $i=1;
    if (strtotime($startDate) < strtotime($endDate))
    {
       while (strtotime($start) < strtotime($endDate))
        {
            $start = date('Y-m-d', strtotime($startDate.'+'.$i.' days'));
            $return[] = $start;
            $i++;
        }
    }

    return $return;
}

结果如下

数组 ([0] => 2016-10-10 [1] => 2016-10-11 [2] => 2016-10-12 [3] => 2016-10-13 [4] => 2016-10-14 [5] => 2016-10-15 [6] => 2016-10-16 [7] => 2016 年 10 月 17 日 [8] => 2016 年 10 月 18 日 [9] => 2016 年 10 月 19 日)

有没有办法使用 PHP 将这些日期中的每一个保存到 MySQL 数据库中?

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    从范围中获取日期后,执行循环以插入数组的每个值。

    $date = getDatesFromRange( "$datef", "$dateto" );
        foreach ($date as $d){
            $sql = "INSERT INTO MyDatabase (dateCol)
                    VALUES ('$d')";
            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }   
    

    【讨论】:

      【解决方案2】:

      您可以使用serialize/unserialize将数组保存在mysql中

      示例 -

      $array = array("my", "litte", "array", 2);
      
      $serialized_array = serialize($array); 
      $unserialized_array = unserialize($serialized_array); 
      
      var_dump($serialized_array); // gives back a string, perfectly for db saving!
      var_dump($unserialized_array); // gives back the array again
      

      您还可以使用 implodeexplode

      例子-

      <?php
          $vegetables[0] = "corn";
          $vegetables[1] = "broccoli";
          $vegetables[2] = "zucchini";
          $text = implode(",", $vegetables);
          echo $text;
      ?>
      
      
      <?php
          $text = "corn, broccoli, zucchini";
          $vegetables = explode(", ", $text);
          print_r($vegetables);
      ?>
      

      【讨论】:

        【解决方案3】:

        您可以使用 json_encode / json_decode 将数组保存在 mysql 中。它易于使用。

        如下代码:

        <?php
            $dates = array(
                 '2016-10-10', '2016-10-11'
            );
            $json_dates = json_encode($dates);
            $arr = json_decode($json_dates, true);
        ?>
        

        【讨论】:

          【解决方案4】:

          您不能将数组存储在数据库中,但替代解决方案是将数组转换为 JSON 字符串。

          您可以将数组转换为 json 并存储到数据库中。

          $dates_in_json = json_encode($dates);
          //Insert query
          $insert = mysql_query("INSERT INTO table('dates') VALUES('".$dates_in_json ."')");
          

          您可以在任何地方获取这些数据并像这样转换为数组

          $dates_array = json_decode($dates_in_json, TRUE);
          

          这里第二个参数TRUE 用于转换成数组,如果不提供TRUE json 将被转换为PHP Object。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-03-23
            • 1970-01-01
            • 1970-01-01
            • 2018-05-08
            • 2012-09-29
            • 1970-01-01
            • 2019-11-22
            • 1970-01-01
            相关资源
            最近更新 更多