implode日期数组并插入MySQL数据库

时间:2022-09-26 08:17:31

I have a major problem trying to implode an array of dates and then inserting them into a database unlike text and integers or doubles which seem easy to do...how do I go about doing this? The date format is "Y/m/d".

我有一个主要的问题,试图破坏一组日期,然后将它们插入数据库,不像文本和整数或双打似乎很容易做...我该怎么做呢?日期格式为“Y / m / d”。

I am imploding various arrays and then inserting them into a database to then later retrieve them and explode them into their original arrays. I want to do this with an array of dates but I need to format them before inserting.

我冒了各种数组,然后将它们插入数据库,然后检索它们并将它们分解成原始数组。我想用一组日期做这个,但我需要在插入之前格式化它们。

How can I format the dates in the array and then pass it to the implode() function to insert it into the MySL database?

如何格式化数组中的日期,然后将其传递给implode()函数以将其插入MySL数据库?

The structure of the array is as follows:

数组的结构如下:

    $array = (
               [0] => 2012/09/13
               [1] => 2008/03/20
               [2] => 2006/12/21
             )

I have tried inserting arrays after imploding the date variable but to no avail. It just prints "Array" in my mysql database - i am using PHPmyadmin - please help me figure out whats going down! thanks!

UPDATES: This is the code I am using:

更新:这是我正在使用的代码:

        $arrlength = count($day);
    $arrlength1 = count($month);
    $arrlength2 = count($year);
$t = 0;
$line = Array();
for($x=0; $x<$arrlength; $x++){
    for($y=0; $y<$arrlength1; $y++){
        for($z=0; $z<$arrlength2; $z++){
            $line = $day[$x]." ".$month[$z]." ".$year[$y];

            }
        }       
    //$line = $day[$x]." 2"; 
    //$x++;
    }
$line = implode(",",$line);
echo $line;

1 个解决方案

#1


0  

You are trying to insert multiple dates into a single DB field of a single row?

您是否尝试将多个日期插入单行的单个数据库字段中?

I would suggest creating an additional table and inserting one row per every date in the array. That would be the best solution and best practice.

我建议创建一个额外的表,并在数组中的每个日期插入一行。那将是最好的解决方案和最佳实践。

But if you want to continue with saving them in a single field/row, use implode()/explode(), or serialize()/unserialize().

但是如果要继续将它们保存在单个字段/行中,请使用implode()/ explode()或serialize()/ unserialize()。

Ok let's sopose you have a field named dates in your table, and it is of type varchar or text with enough length.

好吧,让我们知道你的表中有一个名为dates的字段,它的类型为varchar或文本,长度足够。

<?php

$dates=array('2012/09/13','2008/03/20','2006/12/21');

// save them
$sql="INSERT INTO event(title, dates) VALUES ('dummy title', '".implode(",",$dates)."')";
$conn->query($sql);

// recover them
$sql="SELECT * FROM event";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
  // split them again into array 
  $dates=explode(",",$row["dates"]);
}


?>

But as I said the first time, ideally it would be more correct from a database point of view to create a secondary table.

但正如我第一次说的那样,理想情况下从数据库的角度来看创建辅助表会更正确。

CREATE TABLE IF NOT EXISTS `event` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `event_date` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_id` int(11) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`),
  KEY `event_id` (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

ALTER TABLE `event_date`
  ADD CONSTRAINT `event_date_ibfk_1` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

And ofcourse in this case, before saving a date into event_date, you would first have to convert its format, example:

在这种情况下,在将日期保存到event_date之前,您首先必须转换其格式,例如:

<?php

$original_date='2006/12/21';
$date_object=DateTime::createFromFormat('Y/m/d', $original_date);
if ($date_object) {
  $date_for_mysql=$date_object->format('Y-m-d');
}

?>

#1


0  

You are trying to insert multiple dates into a single DB field of a single row?

您是否尝试将多个日期插入单行的单个数据库字段中?

I would suggest creating an additional table and inserting one row per every date in the array. That would be the best solution and best practice.

我建议创建一个额外的表,并在数组中的每个日期插入一行。那将是最好的解决方案和最佳实践。

But if you want to continue with saving them in a single field/row, use implode()/explode(), or serialize()/unserialize().

但是如果要继续将它们保存在单个字段/行中,请使用implode()/ explode()或serialize()/ unserialize()。

Ok let's sopose you have a field named dates in your table, and it is of type varchar or text with enough length.

好吧,让我们知道你的表中有一个名为dates的字段,它的类型为varchar或文本,长度足够。

<?php

$dates=array('2012/09/13','2008/03/20','2006/12/21');

// save them
$sql="INSERT INTO event(title, dates) VALUES ('dummy title', '".implode(",",$dates)."')";
$conn->query($sql);

// recover them
$sql="SELECT * FROM event";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
  // split them again into array 
  $dates=explode(",",$row["dates"]);
}


?>

But as I said the first time, ideally it would be more correct from a database point of view to create a secondary table.

但正如我第一次说的那样,理想情况下从数据库的角度来看创建辅助表会更正确。

CREATE TABLE IF NOT EXISTS `event` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `event_date` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_id` int(11) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`),
  KEY `event_id` (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

ALTER TABLE `event_date`
  ADD CONSTRAINT `event_date_ibfk_1` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

And ofcourse in this case, before saving a date into event_date, you would first have to convert its format, example:

在这种情况下,在将日期保存到event_date之前,您首先必须转换其格式,例如:

<?php

$original_date='2006/12/21';
$date_object=DateTime::createFromFormat('Y/m/d', $original_date);
if ($date_object) {
  $date_for_mysql=$date_object->format('Y-m-d');
}

?>