Yinks
Yinks

Reputation: 203

Printing(echo) even with null values

so i have 4 tables in which two of them have null values which i believe is the reason for "Catchable fatal error: Object of class variant could not be converted to string".

my tables are

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  

so my code and select statement goes like this

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

is my where statement wrong or my select statement is a little off or what causes the error of "Catchable fatal error: Object of class variant could not be converted to string "

Upvotes: 1

Views: 1605

Answers (1)

Ivan Golović
Ivan Golović

Reputation: 8832

I believe that $rs->Fields("Brand") is returning object which is not of string type and you're trying to concatenate it as if it was string. First you have to cast this object to string or access some property of this object that holds the string data you require.

Instead of $rs->Fields("Brand") try fetching the field data like this:

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

Try traversing the records like this:

while (!$rs->EOF){ 
  echo '<tr><td>'.$rs->fields["Brand"]->value.'-'.$rs->fields["CPU"]->value.'</td></tr>'; 
  $rs->movenext(); 
}

Upvotes: 1

Related Questions