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

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

if (!empty($row[0]))
{
    $something = $row[0];
}

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.

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

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

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

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

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'];
}

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();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.