即使使用空值也可以打印(回显)

时间:2013-02-11 06:56:09

标签: php sql-server select datatable

所以我有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  

所以我的代码和select语句就像这样

<?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语句有点关闭或导致错误“可捕获的致命错误:类变体的对象无法转换为字符串”

1 个答案:

答案 0 :(得分: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(); 
}