1

所以我有 4 个表,其中两个有空值,我认为这是“可捕获的致命错误:类变量对象无法转换为字符串”的原因。

我的桌子是

table1(dbo.FA_PC)  
FAID(pk)  
PCID(fk)  

table2(dbo.PC)  
PCID(PK)  
PCCPUTypeID(fk)(some values are null/empty)  

table3(dbo.PC_CPU_Type)  
PCCPUTypeID(PK)  
CPU  
BrandID(fk)(some values are null/empty)  

table4(Brand)  
BrandID(PK)  
Brand  

所以我的代码和选择语句是这样的

<?php
$faidf=$_POST['faidf'];
ini_set("display_errors","on");
$conn = new COM("ADODB.Connection");
   try {
   $myServer = "WTCPHFILESRV\WTCPHINV";
   $myUser = "sa";
   $myPass = "P@ssw0rd";
   $myDB = "wtcphitinventory";   
   $connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
   $conn->open($connStr); 
         if (! $conn) {
            throw new Exception("Could not connect!");
        }
   }

   catch (Exception $e) {
      echo "Error (File:): ".$e->getMessage()."<br>";
   }
if (!$conn)
  {exit("Connection Failed: " . $conn);}
   $sql_exp = "SELECT  c.CPU, d.Brand
FROM    dbo.FA_PC a
        INNER JOIN dbo.PC b
        on a.PCID = b.PCID
        INNER JOIN dbo.PC_CPU_Type c
            ON b.PCCPUTypeID = c.PCCPUTypeID    
        INNER JOIN dbo.Brand d
            ON c.BrandID = d.BrandID
WHERE   a.FAID = $faidf AND c.PCCPUTypeID is NOT NULL and c.BrandID is NOT NULL";    
   $rs = $conn->Execute($sql_exp);  
    echo "<tr><td>".$rs->Fields("Brand")."-".$rs->Fields("CPU")."</td>";

       $rs->Close();      
?>  

是我的 where 语句错误,还是我的 select 语句有点偏离,或者是什么导致了“可捕获的致命错误:无法将类变量对象转换为字符串”的错误

4

1 回答 1

1

我相信这$rs->Fields("Brand")正在返回不是字符串类型的对象,并且您正试图将它连接起来,就好像它是字符串一样。首先,您必须将此对象转换为字符串或访问此对象的某些属性,该属性包含您需要的字符串数据。

而不是$rs->Fields("Brand")尝试像这样获取字段数据:

$rs->fields["Brand"]->value

尝试像这样遍历记录:

while (!$rs->EOF){ 
  echo '<tr><td>'.$rs->fields["Brand"]->value.'-'.$rs->fields["CPU"]->value.'</td></tr>'; 
  $rs->movenext(); 
}
于 2013-02-11T08:35:15.883 回答