941,512 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7762
  • PHP RSS
Nov 19th, 2007
0

Null values from database causing fatal error

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChazMan is offline Offline
2 posts
since Nov 2007
Nov 19th, 2007
0

Re: Null values from database causing fatal error

empty() should work fine, could you post some code to explain where the problem is?
sample:
php Syntax (Toggle Plain Text)
  1. if (!empty($row[0]))
  2. {
  3. $something = $row[0];
  4. }
Last edited by Nick Evan; Jan 25th, 2011 at 6:42 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 20th, 2007
0

Re: Null values from database causing fatal error

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

The code is:

PHP Syntax (Toggle Plain Text)
  1. $fee = $rs->Fields("Fee");
  2. ....
  3.  
  4. if (empty($fee)) {
  5. $Newfee = "tba";
  6. } else {
  7. $Newfee=$fee;
  8. }
  9. ....
  10. 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.
Last edited by Nick Evan; Jan 25th, 2011 at 6:42 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChazMan is offline Offline
2 posts
since Nov 2007
Apr 10th, 2008
0

Re: Null values from database causing fatal error

Click to Expand / Collapse  Quote originally posted by ChazMan ...
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnyboy999 is offline Offline
2 posts
since Apr 2008
Apr 10th, 2008
0

Re: Null values from database causing fatal error

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)

PHP Syntax (Toggle Plain Text)
  1. if (gettype($var) !== 'string') {
  2. if (settype($var,"string")) {
  3. echo $var . ' set to string';
  4. }
  5. else {
  6. echo $var ' not able to be set to string';
  7. }
  8. }
  9.  
  10. //process variable

I have never had to do this so I don't know if it will work
Last edited by kkeith29; Apr 10th, 2008 at 11:14 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Apr 10th, 2008
0

Re: Null values from database causing fatal error

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

PHP Syntax (Toggle Plain Text)
  1. if (settype($rs->Fields('OR_DELIVERY_INSTRUCTIONS'),"string"))

PHP Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnyboy999 is offline Offline
2 posts
since Apr 2008
Jun 28th, 2008
0

Maybe the solution

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jayetokekule is offline Offline
1 posts
since Jun 2008
Jun 28th, 2008
0

Re: Null values from database causing fatal error

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:
php Syntax (Toggle Plain Text)
  1. if(trim($row['value']) != "")
  2. {
  3. echo $row['value'];
  4. }
Last edited by R0bb0b; Jun 28th, 2008 at 4:17 pm.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Aug 12th, 2009
0

Re: Null values from database causing fatal error

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:

php Syntax (Toggle Plain Text)
  1. $db = new COM("ADODB.Connection");
  2. // custom function to return dsn
  3. $dsn = getDSN();
  4. $db->Open($dsn);
  5.  
  6. $row = array();
  7. $rs = $db->Execute($sql);
  8.  
  9. while (!$rs->EOF)
  10. {
  11. $col = array();
  12. // column length of 20 hardcoded for now
  13. for ($i=0; $i<20; $i++)
  14. {
  15. try
  16. {
  17. $value = $rs->fields[$i]->value;
  18.  
  19. if (trim($value) != "")
  20. {
  21. // check if its a string
  22. if (gettype($value) !== 'string')
  23. {
  24. if (settype($value,"string"))
  25. {
  26. array_push($col, $value);
  27. }
  28. }
  29. else
  30. {
  31. array_push($col, $value);
  32. }
  33. }
  34. else
  35. {
  36. array_push($col, 'NULL');
  37. }
  38. }
  39. catch (exception $e)
  40. {
  41. // break out of loop if we encounter an exception processing the remainder of the row
  42. break;
  43. }
  44.  
  45. }
  46.  
  47. array_push($row, $col);
  48.  
  49. $rs->MoveNext();
  50. }
  51. $rs->Close();
  52.  
  53. $db->Close();
Last edited by jericho034; Aug 12th, 2009 at 1:48 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jericho034 is offline Offline
1 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: making array automatically from database
Next Thread in PHP Forum Timeline: PHP security: user with same IP as the server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC