PHP MS Sql query script for My Sql

Asked

Viewed 77 times

1

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

Looking at this data, you can 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)";
  • Yes, it’s possible that way. Ai you choose whether to do the mysql Insert for each mssql line in a while, or generate a query from a multiples Insert values()

1 answer

0


You can do a mysql Insert for each mssql line this way:

$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 SQL Server and generate a string with a multiples Insert VALUES, thus 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.