implode array of dates and insert into a MySQL database -


i have major problem trying implode array of dates , inserting them database unlike text , integers or doubles seem easy do...how go doing this? date format "y/m/d".

i imploding various arrays , inserting them database later retrieve them , explode them original arrays. want array of dates need format them before inserting.

how can format dates in array , pass implode() function insert mysl database?

the structure of array follows:

    $array = (                [0] => 2012/09/13                [1] => 2008/03/20                [2] => 2006/12/21              )  have tried inserting arrays after imploding date variable no avail. prints "array" in mysql database - using phpmyadmin - please me figure out whats going down! thanks! 

updates: code 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; 

you trying insert multiple dates single db field of single row?

i suggest creating additional table , inserting 1 row per every date in array. best solution , best practice.

but if want continue saving them in single field/row, use implode()/explode(), or serialize()/unserialize().

ok let's sopose have field named dates in table, , of type varchar or text enough length.

<?php  $dates=array('2012/09/13','2008/03/20','2006/12/21');  // save them $sql="insert event(title, dates) values ('dummy title', '".implode(",",$dates)."')"; $conn->query($sql);  // recover them $sql="select * event"; $result = $conn->query($sql); while($row = $result->fetch_assoc()) {   // split them again array    $dates=explode(",",$row["dates"]); }   ?> 

but said first time, ideally more correct database point of view create 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 case, before saving date event_date, first have convert format, example:

<?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'); }  ?> 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -