Click here to Skip to main content
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
Poojanath1.6K

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 mhegazy94 460
1 Sergey Alexandrovich Kryukov 405
2 Kornfeld Eliyahu Peter 285
3 Gihan Liyanage 167
4 Sibeesh KV 150
0 Sergey Alexandrovich Kryukov 6,805
1 OriginalGriff 6,696
2 CPallini 5,350
3 George Jonsson 3,609
4 Gihan Liyanage 2,792


Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2014
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