Solved

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

Posted on 2011-03-16
14
1,307 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

777 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