Solved

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

Posted on 2011-03-16
14
1,316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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