Solved

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

Posted on 2011-03-16
14
1,313 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decrypt string by php 7 47
Number of reviews not counting correctly 2 22
Need to remove %20 from url in get 17 36
ajax to record click 3 16
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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.

828 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