I'm copying data from mssql to php variable and need to save it to mysql.
<html>
<head></head>
<body>
<?php
//MSSql code
$serverName = "exserver"; //serverName\instanceName
$connectionInfo = array( "Database"=>"IPD", "UID"=>"user", "PWD"=>"read");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT rs_date from inc";
$stmt = sqlsrv_query( $conn, $tsql);
//SQL Conn
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "edge_dashboard"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC)) {
$d= $row["0"];
//sql code
$sql = "INSERT INTO critical_sr (sr_lg_dt) VALUES ('$d')";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$con->close();
}
?>
</body>
</html>
I even tried the below:
$d= $row["0"];
$sub_dat =$d->format('Y-m-d H:i:s');
$sql = "INSERT INTO critical_sr (sr_lg_dt) VALUES ('$sub_dat')";
I always encounter this error "Recoverable fatal error: Object of class DateTime could not be converted to string in".
The datatype of row["0"]
in MSSQL is datetime2
. I need it to be saved in datetime format in MySQL.
Thanks for all your help. The solution was simple. I changed the code as below.
$date = $row["0"]; $result = $date->format('Ymd H:i:s');
I had set DateTime size as 6 in MySQL which added unwanted precisions when inserting. I removed the size and it works fine now. No error. Cheers
You can use
$sDat =$d->format('Y-m-d H:i:s');
to create a string-variable and use ti in your insert-statement with
$sql = "INSERT INTO critical_sr (sr_lg_dt) VALUES ('$sDat')";
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.