0

actually i try to write the program from get the ms sql data select and then selected data insert to the MySQL table.i write the program which one get the final value of the table. because insert query place out side the while loop.how do i get the all the value in to the insert query??

I think can store in the while loop execution data json array.can i retrieve the one by one?

MSSQL DB connection

<?php
$serverName = "servername"; //serverName\instanceName, 
portNumber (default is 1433)
$connectionInfo = array( "Database"=>"Databasename", 
"UID"=>"username", "PWD"=>"Password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
 /*echo "Connection established.<br />";*/
}else{
 echo "Connection could not be established.<br />";
 die( print_r( sqlsrv_errors(), true));
}
?>

test.php

<?php 
require_once ('inc/MSSQL_connection.php');
//require_once ('inc/MYSQL_connection.php');


     $query ="SELECT EnrolledID,Date,Time FROM dbo.view_attendance
              ORDER BY Date,Time";                    
     $result = sqlsrv_query($conn,$query);
 if ($result == FALSE){
       die(FormatErrors(sqlsrv_errors()));
   }
 else{

      while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))  
     { 

        $emp_no = $row['EnrolledID'];
        $date  = date_format($row['Date'], 'Y-m-d');
        $time = date_format($row['Time'], 'H:i:s');
     }
       //echo $emp_no;
       $conn = mysqli_connect("localhost", "root", "","kelaniya");
       $query = "INSERT INTO attendancedata (EnrolledID,Date,Time) VALUES ('{$emp_no}', '{$date}', '{$time}')";
       mysqli_query($conn, $query); 

  }  
 ?>
Nipun Sachinda
  • 430
  • 2
  • 14

1 Answers1

0

IMHO you can just move insert into while loop, why not?:

    <?php 
    require_once ('inc/MSSQL_connection.php');
    //require_once ('inc/MYSQL_connection.php');


    $query ="SELECT EnrolledID,Date,Time FROM dbo.view_attendance
                  ORDER BY Date,Time";                    
    $result = sqlsrv_query($conn,$query);
    if ($result == FALSE){
           die(FormatErrors(sqlsrv_errors()));
    }
    else{
          $conn_insert = mysqli_connect("localhost", "root", "","kelaniya");
          while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))  
           { 

            $emp_no = $row['EnrolledID'];
            $date  = date_format($row['Date'], 'Y-m-d');
            $time = date_format($row['Time'], 'H:i:s');
           //echo $emp_no;

           // MySQLli

           $stmt = $conn_insert->prepare('INSERT INTO attendancedata (EnrolledID,Date,Time) VALUES (?,?,?)');
           // I insert as string as was in original question (table metadata was not given...)
           $stmt->bind_param('sss', $emp_no, $date, $time ); // 's' specifies the variable type => 'string'
           $stmt->execute();
           }
      }  
     ?>
Alex Martian
  • 3,423
  • 7
  • 36
  • 71