PHP script script from MS Sql for My Sql

1

Good afternoon. How can I make a query in MS Sql and insert the values into a My Sql table through PHP?

Looking at this data, can you do this insertion?

    #--> conexao mysql
    require_once("mysql.php");
    #--> conexao mssql
    require_once("mssql.php");
    #--> consulta sql server
    $sqlserver= "select * from sql_server_table";
    $sqlserverResult = odbc_exec($conMsSql, $sqlserver);
    #--> insere no MySql
    mysql_query("insert into mysql_table (col1, col2, col3) values ($sqlserverResult)";
    
asked by anonymous 23.10.2015 / 18:42

1 answer

0

You can do an insert in mysql for each mssql line like this:

$sql = "SELECT c1, c2, c3, c4 FROM mssql_table";

$result = odbc_exec($conn, $sql);
while($row = odbc_fetch_array($result)){
        $insert = sprintf("INSERT INTO mysql_table(c1, c2, c3, c4) VALUES('%s', '%s', '%s', '%s')",
                            $row['c1'], $row['c2'], $row['c3'], $row['c4']);

        if(!mysql_query($insert)){
            echo 'linha com id: '. $row['c1'] , ' não foi inserida';
        }
    }
}

Or you can query in SQL Server and generate a string with an insert to multiple VALUES , that way all records will be entered or none of them.

$sql = "SELECT c1, c2, c3, c4 FROM mssql_table";

$insert = "INSERT INTO mysql_table(c1, c2, c3, c4) VALUES ";

$result = odbc_exec($conn, $sql);
while($row = odbc_fetch_array($result)){
    $insert .= sprintf(" ('%s', '%s', '%s', '%s'),", $row['c1'], $row['c2'], $row['c3'], $row['c3']);     
}

$insert = trim($insert, ','); //remove a ultima virgula

if(!mysql_query($insert)){
    echo 'nenhum registro foi inserido. Erro: '. mysql_error();
}
    
23.10.2015 / 18:57