0

我想从 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

4

2 回答 2

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);
?>

谢谢瑞恩的帮助,我在编辑问题时似乎想到了解决方案。再次感谢。

于 2014-03-11T09:38:31.367 回答
0

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

$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);
?>
于 2014-03-10T13:33:31.260 回答