Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What will be the best practice to fetch data from MSSQL and insert/update to MySQL?

So, i’d like to fetch data from MSSQL and insert/update in MySQL.

I have this script:

//QUERY
$sql = "SELECT id, ordernr FROM orders";

//EXECUTE
$stmt = sqlsrv_query( $conn, $sql );

//ERROR?
if( $stmt === false) {
  die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {}

Should i create an array and insert whole the array in MySQL, is that possible?
Or should i iterate the fetched array and insert it into MySQL row by row?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have this in mind:

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  $query = "INSERT INTO table
            SET ordernr = $row['ordernr']
            ON DUPLICATE KEY UPDATE
            ordernr = $row['ordernr']";

  EXECUTE $qurey... bla bla
});

What will be the best way?

>Solution :

Use a prepared statement that you prepare once, outside the loop.

$stmt = $conn->prepare("
    INSERT INTO TABLE (id, ordernr) 
    VALUES (?, ?) 
    ON DUPLICATE KEY UPDATE ordernr = VALUES(ordernr)");
$stmt->bind_param($id, $ordernr);

while ($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    $id = $row['id'];
    $ordernr = $row['ordernr'];
    $stmt->execute();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading