简体   繁体   中英

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:

<?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. 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.

To begin with, your code has several issues. A decent IDE would show that to you in the editor.

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.

    $id_product = $row[prod_id];

There is no constant prod_id . You mean $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'] .

    $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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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