Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-16
14
Medium Priority
?
1,329 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.

688 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