Solved

Catchable fatal error: Object of class variant could not be converted to string error in PHP

Posted on 2011-03-16
14
1,300 Views
Last Modified: 2012-05-11
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:

<?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.
0
Comment
Question by:dzirkelb
  • 9
  • 5
14 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35151211
try:
1. adding error_reporting(E_ALL); to the top of the script
2. checking if $strOldTariffCode === NULL
3. checking is_object($strOldTariffCode)
0
 

Author Comment

by:dzirkelb
ID: 35151271
options 1 and 2 produced the same error.

Option 3 returns true, so it gives the value of $strOldTariffCode as "Test" (minus the quotes).

unfortunately I will not be able to use that code as when it does have a value, then it will change it's value to the "Test" when I don't want it to.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35151338
Try this to see what the variable look like:

if (is_object($strOldTariffCode))
{
      var_dump($strOldTariffCode);
}
0
 

Author Comment

by:dzirkelb
ID: 35151360
object(variant)#3 (0) { }  is what is produced.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35151395
http://php.net/manual/en/function.variant-get-type.php

try this below to see what is the variant type
if (is_object($strOldTariffCode))
{
      $test = variant_get_type($strOldTariffCode);
      echo"$test";
}
0
 

Author Comment

by:dzirkelb
ID: 35151425
it returns 9
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35151536
OK, then try to compare it like this:

if(variant_get_type($strOldTariffCode) == VT_NULL)
{
  echo"this is null";
}
else{
   echo"not null !";
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dzirkelb
ID: 35151602
This returns not null !

However, it is incorrect as it should be null.

I tested this with another variable in my code from above, $intID, which after running the query returns the value of 146553.

So, in summary, this is what is happening with the new code just provided:

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

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

returns not null !
0
 

Author Comment

by:dzirkelb
ID: 35151616
sorry, forgot to produce a result:

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

returns not null !
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35151696
seems like its a type of VT_DISPATCH
http://php-manual.skryptoteka.pl/com.constants.html
try - just to check

if(variant_get_type($strOldTariffCode) == VT_DISPATCH)
{
  echo"this is pointer";
}
else{
   echo"not pointer !";
}
0
 

Author Comment

by:dzirkelb
ID: 35151874
it prints this is pointer
0
 

Author Comment

by:dzirkelb
ID: 35151882
Also,  the backed end database is sql 2000 and the server is windows 2003 running iis 6.0 if that helps at all.
0
 

Author Comment

by:dzirkelb
ID: 35156077
echo $strOldTariffCode->value
if (is_null($strOldTariffCode->value))
var_dump($strOldTariffCode->value)

all produce the following error if this helps:

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
0
 

Author Comment

by:dzirkelb
ID: 35157340
Thanks for all of your help.  I figured out that doing $strOldTariffCode = $rs['TariffCode']->value; produces the results I am looking for.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now