954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Null values from database causing fatal error

I have an Access database (yetch) from which I am retrieving a recordset. One of the text fields in the recordset has a few null values. I need to display all records (so I can't just eliminate these records in the SQL), but display a blank or alternative string for the null values.

If I try any string handling function on the null-valued variable, it chokes and gives me this:

Catchable fatal error: Object of class variant could not be converted to string in C:\Inetpub\wwwroot\Comed2005\category.php on line 193

I have no control over the database (long story), so I can't change the default value for the field.

I have tried inspecting the variable using empty() isset() is_null() ===null and a few other home-grown functions that I have found on other forums. None of these seem to be able to tell the difference between the null values, and those with something in them. I also tried using if(method_exists($fee, "__tostring")) but with no joy. The variables are all variant objects, and I can only tell which ones are null because all string functions choke on them.

Any suggestions gratefully received...

ChazMan
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

empty() should work fine, could you post some code to explain where the problem is?
sample:

if (!empty($row[0]))
{
    $something = $row[0];
}
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

I agree Nick, empty() should work. But doesn't.

The code is:

$fee = $rs->Fields("Fee");
....
		
if (empty($fee)) {
    $Newfee = "tba";
} else {
    $Newfee=$fee;
}
....
echo $Newfee;


which gives me the error:
Catchable fatal error: Object of class variant could not be converted to string in C:\Inetpub\wwwroot\Comed2005\category.php on line 193

Line 193 is the echo $Newfee line.

ChazMan
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

I have an Access database (yetch) from which I am retrieving a recordset. One of the text fields in the recordset has a few null values. I need to display all records (so I can't just eliminate these records in the SQL), but display a blank or alternative string for the null values.

If I try any string handling function on the null-valued variable, it chokes and gives me this:

Catchable fatal error: Object of class variant could not be converted to string in C:\Inetpub\wwwroot\Comed2005\category.php on line 193

I have no control over the database (long story), so I can't change the default value for the field.

I have tried inspecting the variable using empty() isset() is_null() ===null and a few other home-grown functions that I have found on other forums. None of these seem to be able to tell the difference between the null values, and those with something in them. I also tried using if(method_exists($fee, "__tostring")) but with no joy. The variables are all variant objects, and I can only tell which ones are null because all string functions choke on them.

Any suggestions gratefully received...

Hi, I also have this problem, and wondered whether either a) you managed to fix it or b) whether someone out there knows how to resolve the problem.

Any help would be really appreciated...

jonnyboy999
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

i think the item you are evaluating is an object and needs to converted to a string to be read like that.

maybe this will work (just a guess)

if (gettype($var) !== 'string') {
  if (settype($var,"string")) {
    echo $var . ' set to string';
  }
  else {
    echo $var ' not able to be set to string';
  }
}

//process variable


I have never had to do this so I don't know if it will work

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Hi, Thanks for replying with the suggestion. Unfortunately I get the same error on the following line:

if (settype($rs->Fields('OR_DELIVERY_INSTRUCTIONS'),"string"))
PHP Catchable fatal error: Object of class variant could not be converted to string in


I wonder whether the issue is due to the version of php I am running, and potentially the version of the libmysql.dll? As I did have a problem with the initial php installation (php-5.2.5-win32-installer.msi) when trying to access an access database. The resolution was to replace the installed libmysql.dll with the version contained within (php-5.2.1-Win32.zip).

Inserts, Updates, Selects all work fine for reference....

Thanks J

jonnyboy999
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Hi, I hope it is not too late, but I think you need to access the value in the record, in this way:

$rs["fieldname"]->value

or maybe $rs->Fields["fieldname"]->value

I hope it helps

jayetokekule
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

The one thing that I have started doing and it seems to work very well for both blank strings and null values coming out of mysql and postgresql(haven't tried access) is:

if(trim($row['value']) != "")
{
         echo $row['value'];
}
R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

I know this thread is a bit old (circa 2008) but it was very useful for me so I'll paste my solution (which the thread posters incorporated) above. Hope that helps someone:

$db = new COM("ADODB.Connection");
// custom function to return dsn
		$dsn = getDSN();
		$db->Open($dsn);

		$row = array();
		$rs = $db->Execute($sql);

		while (!$rs->EOF)
		{
			$col = array();
// column length of 20 hardcoded for now
			for ($i=0; $i<20; $i++)
			{
				try
				{
					$value = $rs->fields[$i]->value;

					if (trim($value) != "")
					{
						// check if its a string
						if (gettype($value) !== 'string')
						{
							if (settype($value,"string"))
							{
								array_push($col, $value);
							}
						}
						else
						{
							array_push($col, $value);
						}
					}
					else
					{
						array_push($col, 'NULL');
					}
				}
				catch (exception $e)
				{
// break out of loop if we encounter an exception processing the remainder of the row
					break;
				}

			}

			array_push($row, $col);

			$rs->MoveNext();
		}
		$rs->Close();

		$db->Close();
jericho034
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You