User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,609 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2056 | Replies: 7
Reply
Join Date: Nov 2007
Posts: 2
Reputation: ChazMan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ChazMan ChazMan is offline Offline
Newbie Poster

Null values from database causing fatal error

  #1  
Nov 19th, 2007
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...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,816
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 189
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Null values from database causing fatal error

  #2  
Nov 19th, 2007
empty() should work fine, could you post some code to explain where the problem is?
sample:
  1. if (!empty($row[0]))
  2. {
  3. $something = $row[0];
  4. }
regards Niek
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Nov 2007
Posts: 2
Reputation: ChazMan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ChazMan ChazMan is offline Offline
Newbie Poster

Re: Null values from database causing fatal error

  #3  
Nov 20th, 2007
I agree Niek, 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.
Reply With Quote  
Join Date: Apr 2008
Posts: 2
Reputation: jonnyboy999 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonnyboy999 jonnyboy999 is offline Offline
Newbie Poster

Re: Null values from database causing fatal error

  #4  
Apr 10th, 2008
Originally Posted by ChazMan View 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...


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...
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: Null values from database causing fatal error

  #5  
Apr 10th, 2008
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
Last edited by kkeith29 : Apr 10th, 2008 at 11:14 am.
Reply With Quote  
Join Date: Apr 2008
Posts: 2
Reputation: jonnyboy999 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonnyboy999 jonnyboy999 is offline Offline
Newbie Poster

Re: Null values from database causing fatal error

  #6  
Apr 10th, 2008
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
Reply With Quote  
Join Date: Jun 2008
Posts: 1
Reputation: jayetokekule is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jayetokekule jayetokekule is offline Offline
Newbie Poster

Maybe the solution

  #7  
Jun 28th, 2008
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
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 850
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 66
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Null values from database causing fatal error

  #8  
Jun 28th, 2008
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:
  1. if(trim($row['value']) != "")
  2. {
  3. echo $row['value'];
  4. }
Last edited by R0bb0b : Jun 28th, 2008 at 4:17 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC