如何从Firebird数据库读取并将数据插入MSSQL数据库?

时间:2014-03-10 10:43:21

标签: php mysql sql sql-server sqlsrv

我想从Firebird数据库中选择一些列并将它们插入MSSQL数据库表中。

当我从Firebird读取表格时,它代表我在localhost中需要的内容,然后当我想将数据插入MSSQL时,它只插入最后一条记录。我不知道是什么原因引起的,这是我第一次尝试这个想法。

有人会告诉我出了什么问题吗?

这是我的PHP代码:

<?php
$host = 'localhost:c:\firebird.fdb'; 
$username=''; 
$password='';

$dbh = ibase_connect($host, $username, $password);
$stmt = "SELECT * FROM TWEEKDAY";
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
$R1 = $row->CODE;
$R2 = $row->DAYNAME;
    echo $R1 ." ". $R2  . "\n";
    echo "</br>";
}
ibase_close($dbh);

/**********************************************/

$host = "servername\instancename";
$connectionInfo = array( "Database"=>"MSSQLdatabase", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}
echo "</br>"; 

$tsql =  "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1','$R2')";

$stmt = sqlsrv_query( $conn, $tsql); 

if ( $stmt === false )    {    
     die( print_r( sqlsrv_errors(), true));    
} 
 while( $obj = sqlsrv_fetch_object( $stmt ,  SQLSRV_FETCH_ASSOC) ) {
      echo $obj->DAYID ."&nbsp ". $obj->DAYN . "<br />";
}
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);
?>

当我运行我的代码时,它在本地主机中显示为: PIC1 当我打开mssql数据库时,该表有什么: PIC2 它似乎插入$ R1 $ R2的最后一个值。

当我使用此代码时:

<?php
$host = 'localhost:c:\FIREBIRD.fdb'; 
$username=''; 
$password='';

$dbh = ibase_connect($host, $username, $password);
$stmt = "SELECT * FROM TWEEKDAY";
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
$R1 = $row->CODE;
$R2 = $row->DAYNAME;
    echo $R1." ". $R2  . "\n";
    echo "</br>";
}
ibase_close($dbh);

/**********************************************/

$host = "SERVERNAME\INSTANCENAME";
$connectionInfo = array( "Database"=>"AccessCard", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}
echo "</br>"; 

for ($RIdx = 0; $RIdx < count($R1); $RIdx++) { // each $R array value

  $tsql =  "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1[$RIdx]','$R2[$RIdx]')"; 
  $stmt = sqlsrv_query( $conn, $tsql); 
  if ( $stmt === false )    {    
     die( print_r( sqlsrv_errors(), true));    
  }
} // end of insert loop 

sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);
?>

这是数据库中显示的内容: PIC3

2 个答案:

答案 0 :(得分:0)

$ R1应该是一个数组,如$ R1 [] = $ row-&gt; CODE;显然也是$ R2'。相应地更改sqlserver插入。我这里没有安装sql server所以我无法提供经过测试的代码。 这是sql server代码......

$host = "servername\instancename";
$connectionInfo = array( "Database"=>"MSSQLdatabase", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}
echo "</br>"; 

for ($RIdx = 0; $RIdx < count($R1); $RIdx++) { // each $R array value

  $tsql =  "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1[$RIdx]','$R2[$RIdx]')"; 

  $stmt = sqlsrv_query( $conn, $tsql); 

  if ( $stmt === false )    {    
     die( print_r( sqlsrv_errors(), true));    
  }

} // end of insert loop 


sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);
?>

答案 1 :(得分:0)

最终问题解决了: 这是解决方案代码

<?php
$host = 'localhost:c:\firebird.fdb'; 
$username=''; 
$password='';

$dbh = ibase_connect($host, $username, $password);
$stmt = "SELECT * FROM TWEEKDAY";

/**********************************************/

$host = "servername\instancename";
$connectionInfo = array( "Database"=>"AccessCard", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}
echo "</br>"; 

/**********************************************/

$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
$R1 = $row->CODE;
$R2 = $row->DAYNAME;
    echo $R1." ". $R2  . "\n";
    $tsql = "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1','$R2')";
    $stmt = sqlsrv_query( $conn, $tsql); 

if ( $stmt === false )    {    
     die( print_r( sqlsrv_errors(), true));    
} 
while( $obj = sqlsrv_fetch_OBJECT( $stmt) ) {
      echo $obj->$R1."&nbsp ". $obj->$R2. "<br />";
      $obj++; 
}
    echo "</br>";
}
ibase_close($dbh);
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);
?>
谢谢Ryan的帮助,在我编辑我的问题时似乎想到了解决方案。 再次感谢。