Click here to Skip to main content
11,706,639 members (31,366 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
I am geeting some records from ms sql server using php and I need to insert those
in to mysql database table.

 <?php
// create connection
$connection = mssql_connect("server-ip","user-name","password");
 
// test connection
if (!$connection) {
echo "Couldn't make a connection!";
exit;
}
 
// select database
$db = mssql_select_db("db-name", $connection);
 
// test selection
if (!$db) {
echo "Couldn't select database!";
exit;
}
 
// create SQL statement
$sql = "SELECT * from DBTable";
 
// execute SQL query and get result
$sql_result = mssql_query($sql,$connection);
 
// start results formatting
echo "<TABLE BORDER=1>";
echo "<TR><TH>Product No</TH><TH>Product Name</TH>";
 
// format results by row
while ($row = mssql_fetch_array($sql_result)) {
$po_no = $row["PNumber"];
$po_name = $row["PName"];
echo "<TR><TD>$po_no</TD></TR>";
echo "<TR><TD>$po_name</TD></TR>";
 
//Here I need to write code to insert these details in to
//my sql database.Is this way is correct?
// Section My SQL

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("my_db", $con);
 
mysql_query("INSERT INTO mysqlProducts (ProductNo,ProductName)
VALUES ($po_no,$po_name)");
 

mysql_close($con);
 
}
 
echo "</TABLE>";
 
// free resources and close connection
mssql_free_result($sql_result);
mssql_close($connection);
 
?> 

Here I need to write code to insert these details in to
my sql database.Is this way is correct? please see the section
// Section My SQL

Thanks
Posted 29-Dec-12 21:16pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

One option is to select every row from the one database, and insert into the other. But don't recreate connection for every insert, do it once before loop

There are other options too if you have administrative privileges on the SQL server, and it is good for you to push data from sql server to mysql: http://forums.mysql.com/read.php?60,123221,123221[^].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 146
1 Sergey Alexandrovich Kryukov 115
2 chainerlt 110
3 Mika Wendelius 80
4 ppolymorphe 71
0 OriginalGriff 9,190
1 Sergey Alexandrovich Kryukov 8,407
2 CPallini 5,189
3 Maciej Los 4,756
4 Mika Wendelius 3,686


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100