Catchable fatal error: Object of class variant could not be

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dzirkelb
Forum Newbie
Posts: 15
Joined: Wed Dec 02, 2009 8:52 am

Catchable fatal error: Object of class variant could not be

Post by dzirkelb »

I am trying some basic code, but am producing the error message in the subject line above. It is returning this error because the result is a NULL value; however, I have tried using empty and is_null and it still produces the same error. Here is some relevant code:

Code: Select all

<?php
require("dbQS.php");

$strPart = CheckPost('hidPart', '');
$strCountryOfOrigin = CheckPost('txtCountryOfOrigin', '');
$intPO = CheckPost('hidPO', '');
$strMfg = CheckPost('hidMfg', '');
$strTariffCode = trim(CheckPost('txtTariffCode', ''));

$ssql = "SELECT TariffCode, ID FROM InventoryMaster1 WHERE ([PART #] = '$strPart')";
$rs = $dbc->execute($ssql);

if (!$rs->EOF)
{
      $strOldTariffCode = $rs['TariffCode'];
      $intID = $rs['ID'];
}else{
      $strOldTariffCode = "";
      $intID = "";
}

$rs->close;
$rs=null;

if (is_null($strOldTariffCode))
{
      $strOldTariffCode = "Test";
}

echo $strOldTariffCode;
The error occurs on the echo $strOldTariffCode; line.

I have replaced the is_null with empty and it produced the same error. I have also changed it to if (trim($strOldTariffCode) == NULL) and it still produces a seperate error of:

Warning: trim() expects parameter 1 to be string, null given

Any ideas on how to get past this? Basically, I just want the result ot be a blank value if it is a NULL value
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Catchable fatal error: Object of class variant could not

Post by Darhazer »

There is nothing wrong with echo of null value.
What version of PHP is this.
Can you please run var_dump( $strOldTariffCode );
Are you sure you are not passing this variable to some function / method

trim can handle null values as well.
dzirkelb
Forum Newbie
Posts: 15
Joined: Wed Dec 02, 2009 8:52 am

Re: Catchable fatal error: Object of class variant could not

Post by dzirkelb »

Code: Select all

if (is_object($strOldTariffCode))
{
      $test = variant_get_type($strOldTariffCode);
      echo"$test";
} 
produces 9

Code: Select all

if (is_object($strOldTariffCode))
{
      var_dump($strOldTariffCode);
} 
produces object(variant)#3 (0) { }

Code: Select all

$strOldTariffCode === NULL
produces same results as subject line

Where do I find the php version? This is running under iis 6.0. it was installed about a year ago with whatever the latest php version was at that time.

Also, i tried the following:

Code: Select all

$strOldTariffCode is equal to NULL
if(variant_get_type($strOldTariffCode) == VT_NULL)
{
  echo"this is null";
}
else{
   echo"not null !";
}
returns not null !

$intID is equal to 146553

Code: Select all

if(variant_get_type($intID) == VT_NULL)
{
  echo"this is null";
}
else{
   echo"not null !";
}
returns not null !
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Catchable fatal error: Object of class variant could not

Post by Darhazer »

You are not working with null, but with an instance of variant
why do you think you are working with null?
9 is VT_DISPATCH - A pointer to a pointer to an object was specified.
It's not even a VT_EMPTY or VT_NULL object

So you cannot echo the object, since it has no __toString() method
dzirkelb
Forum Newbie
Posts: 15
Joined: Wed Dec 02, 2009 8:52 am

Re: Catchable fatal error: Object of class variant could not

Post by dzirkelb »

I figured it was null because when running the query in sql manager, it shows null as the value. And, because when I try to use trim it says it is expecting a value and it returned null instead.

regardless, I need the ability to use this variable later on in the code. I need to be able to echo the results. If it is null, or whatever it is, then it would echo nothing.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Catchable fatal error: Object of class variant could not

Post by Darhazer »

try

Code: Select all

echo $strOldTariffCode->value
Respectively:

Code: Select all

if (is_null($strOldTariffCode->value))
and

Code: Select all

var_dump($strOldTariffCode->value)
dzirkelb
Forum Newbie
Posts: 15
Joined: Wed Dec 02, 2009 8:52 am

Re: Catchable fatal error: Object of class variant could not

Post by dzirkelb »

They all produce the following error:

Fatal error: Uncaught exception 'com_exception' with message 'Source: ADODB.Field Description: Object is no longer valid.' in D:\xxx.php:26 Stack trace: #0 D:\xxx-save.php(26): unknown() #1 {main} thrown in D:\xxx-save.php on line 26
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Catchable fatal error: Object of class variant could not

Post by Darhazer »

Ok, try this one (honestly I didn't work with either com, addodb or variant)

Code: Select all

if (!$rs->EOF)
{
$strOldTariffCode = $rs['TariffCode']->value;
$intID = $rs['ID']; // maybe you need here $rs['ID']->value as well
}else{
$strOldTariffCode = "";
$intID = "";
}
dzirkelb
Forum Newbie
Posts: 15
Joined: Wed Dec 02, 2009 8:52 am

Re: Catchable fatal error: Object of class variant could not

Post by dzirkelb »

Bingo! That did the trick, thank for the help!
Post Reply