简体   繁体   English

将日期从一个表复制到另一个表并在mysql中更改格式

[英]Copying date from one table to another and change format in mysql

I am trying to copy a date (Ymd) from one database table to another database table with format (Ymd H:i:s), with the following code: 我正在尝试使用以下代码将日期(Ymd)从一个数据库表复制到格式为(Ymd H:i:s)的另一数据库表:

<?php
  $host = "host";
  $user = "user";
  $password = "pass";
  $database1 = "home";
  $database2 = "teste";

  $con1 = mysqli_connect($host, $user, $password, $database1);
  mysqli_set_charset($con1, 'utf8');
  if (!$con1) {die(mysqli_connect_error($con1));}

  $select = "SELECT * FROM table1";
  $result = mysqli_query($con1, $select) or die(mysqli_error($con1));

  while($row = mysqli_fetch_assoc($result)) {

    $id_product = $row[prod_id];
    $date = date_create_from_format('Y-m-d', '$row[date]');
    $date_add = date_format($date, 'Y-m-d H:i:s');

    $con2 = mysqli_connect($host, $user, $password, $database2);
    mysqli_set_charset($con2, 'utf8');
    if (!$con2) {die(mysqli_connect_error($con2));}

    $ins = "INSERT INTO `table2` (id_product, date_add) VALUES ('$id_product',$date_add)";
    $mysq = mysqli_query($con2, $ins) or die(mysqli_error($con2));
  }
?>

This "INSERT" code is not working because on the table2 the date added is "0000-00-00 00:00:00" and I am sure that the date on table1 is, for example, 2017-02-13. 此“ INSERT”代码不起作用,因为在table2上添加的日期是“ 0000-00-00 00:00:00”,并且我确定table1上的日期是例如2017-02-13。 Is possible to check where is my code wrong? 可以检查我的代码在哪里错误? I have checked other questions almost same problem and follow them but my result never worked. 我检查了几乎相同的其他问题,并按照它们进行操作,但是我的结果从未奏效。

Thank you 谢谢

You can just do: 您可以这样做:

INSERT INTO `table2` (id_product, date_add)
    SELECT prod_id, date
    FROM table1;

If date is a date data type and date_add is datetime , then the time portion will be added and set to midnight automatically. 如果datedate数据类型,且date_adddatetime ,则将添加时间部分并将其自动设置为午夜。

To begin with, your code has several issues. 首先,您的代码有几个问题。 A decent IDE would show that to you in the editor. 一个不错的IDE会在编辑器中向您显示。

Code Issues 代码问题

mysqli_set_charset($con1, 'utf8');

You are trying to set the charset using the connection, before you make sure that the connection was established. 在确保建立连接之前,您尝试使用连接设置字符集。

if (!$con1)
{
    die(mysqli_connect_error($con1));
}

mysqli_connect_error() does not take any parameter, $con1 is false anyway. mysqli_connect_error()不带任何参数, $con1false

    $id_product = $row[prod_id];

There is no constant prod_id . 没有常量prod_id You mean $id_product = $row['prod_id']; 您的意思是$id_product = $row['prod_id']; instead. 代替。

    $date       = date_create_from_format('Y-m-d', '$row[date]');

You are trying to generate a date from the literal string '$row[date]' and not from the date you got from the database, which is $row['date'] . 您试图从文字字符串'$row[date]'生成日期,而不是从数据库中获取的日期$row['date']

    $con2 = mysqli_connect($host, $user, $password, $database2);

You are creating a new database connection for each single record, without closing it after use. 您正在为每个单个记录创建一个新的数据库连接,而不在使用后关闭它。

    $ins = "INSERT INTO `table2` (id_product, date_add) VALUES ('$id_product',$date_add)";

$date_add is a string, it needs to be quoted. $date_add是一个字符串,需要用引号引起来。

Your Problem 你的问题

If you are using the correct types in your database tables, MySQL will add 00:00:00 automatically to the date, so you don't need to address that directly, as already stated by Gordon Linoff. 如果您在数据库表中使用正确的类型,MySQL会在日期上自动添加00:00:00 ,因此您无需直接解决该问题,正如Gordon Linoff所说。

If that's not the case, the date still is transferred as a string, so simple string concatenation solves the problem. 如果不是这种情况,则日期仍将作为字符串传输,因此简单的字符串连接解决了该问题。

$date_add = $row['date'] . ' 00:00:00';

The Refactored Code 重构代码

After the proposed cleanup, your code looks like this (and do what you want): 提议的清除后,您的代码如下所示(并执行您想要的操作):

<?php
$host      = "host";
$user      = "user";
$password  = "pass";
$database1 = "home";
$database2 = "teste";

$source = mysqli_connect($host, $user, $password, $database1) or die(mysqli_connect_error());
mysqli_set_charset($source, 'utf8');

$target = mysqli_connect($host, $user, $password, $database2) or die(mysqli_connect_error());
mysqli_set_charset($target, 'utf8');

$sql    = "SELECT * FROM table1";
$result = mysqli_query($source, $sql) or die(mysqli_error($source));

while ($row = mysqli_fetch_assoc($result))
{
    $id   = $row['prod_id'];
    $date = $row['date'] . ' 00:00:00';
    $sql  = "INSERT INTO `table2` (id_product, date_add) VALUES ('$id','$date')";
    mysqli_query($target, $sql) or die(mysqli_error($target));
}

mysqli_close($target);
mysqli_close($source);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM