-> [PHP5]: Problem with mysql_fetch_object trying to convert to int???

This is a discussion on -> [PHP5]: Problem with mysql_fetch_object trying to convert to int??? within the PHP Language forums, part of the PHP Programming Forums category; Hi, I am currently moving a PHP4 application to PHP5 and I run across a strange problem and don't ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-26-2008
Steve JORDI
 
Posts: n/a
Default -> [PHP5]: Problem with mysql_fetch_object trying to convert to int???

Hi,

I am currently moving a PHP4 application to PHP5 and I run across a
strange problem and don't understand why it causes error messages.

Here is what I have.
In a class I have the following

class dbConnection {
private $result ;
private $linkID ;
private $query ;
public $row ;
static private $instance ; // Singleton
...

function &getRow() {
if( !($this->row = mysql_fetch_object($this->result)) )
$this->row = 0 ;
return $this->row ;
}
}

Then in my application I have the following (it's in another class):

class Welcome {

private $db = 0 ;
...
$this->db = dbConnection::getInstance() ;
$this->db->connect("") ;
...


// Parse data until no more rows are available
while( ($this->row = $this->db->getRow() ) != 0 ) {
$this->myVar1 = $this->row->theVar1 ;
$this->myVar2 = $this->row->theVar2 ;
/// blah blah
}

The problem is on while line.
The error message is
Notice: Object of class stdClass could not be converted to int in
/[...]/jorditests/[myappname]/class_welcome.php on line 270


This works without problem in PHP4. Not in PHP5.
In PHP5, I then can get the data out of the $row variable and the
result is correct. But why the convertion Notice warning?

(Note that in PHP4 I have MySQL 4, and on the PHP5 server, MySQL 5)

Any clue why?


Thanks for any help.

Sincerely,
Steve JORDI

(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
Reply With Quote
  #2 (permalink)  
Old 02-26-2008
Jerry Stuckle
 
Posts: n/a
Default Re: -> [PHP5]: Problem with mysql_fetch_object trying to convertto int???

Steve JORDI wrote:
> Hi,
>
> I am currently moving a PHP4 application to PHP5 and I run across a
> strange problem and don't understand why it causes error messages.
>
> Here is what I have.
> In a class I have the following
>
> class dbConnection {
> private $result ;
> private $linkID ;
> private $query ;
> public $row ;
> static private $instance ; // Singleton
> ...
>
> function &getRow() {
> if( !($this->row = mysql_fetch_object($this->result)) )
> $this->row = 0 ;
> return $this->row ;
> }
> }
>
> Then in my application I have the following (it's in another class):
>
> class Welcome {
>
> private $db = 0 ;
> ...
> $this->db = dbConnection::getInstance() ;
> $this->db->connect("") ;
> ...
>
>
> // Parse data until no more rows are available
> while( ($this->row = $this->db->getRow() ) != 0 ) {
> $this->myVar1 = $this->row->theVar1 ;
> $this->myVar2 = $this->row->theVar2 ;
> /// blah blah
> }
>
> The problem is on while line.
> The error message is
> Notice: Object of class stdClass could not be converted to int in
> /[...]/jorditests/[myappname]/class_welcome.php on line 270
>
>
> This works without problem in PHP4. Not in PHP5.
> In PHP5, I then can get the data out of the $row variable and the
> result is correct. But why the convertion Notice warning?
>
> (Note that in PHP4 I have MySQL 4, and on the PHP5 server, MySQL 5)
>
> Any clue why?
>
>
> Thanks for any help.
>
> Sincerely,
> Steve JORDI
>
> (Remove the K_I_L_LSPAM from my email address)
> ------------------------------------------------
> 1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
> Switzerland WWW: www.sjordi.com
> ------------------------------------------------
> Volcanoes at www.sjordi.com/volcanoes
> MovieDB at www.sjmoviedb.com
> ------------------------------------------------
>


The message is correct. You're checking an object returned by your
getRow() function and then attempting to compare that object to integer
zero. There is no conversion for the two.

Rather, try returning false instead of zero from your function, and just
checking for false/not false, i.e.

while($this->row = $this->db->getRow())

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #3 (permalink)  
Old 02-26-2008
Toby A Inkster
 
Posts: n/a
Default Re: -> [PHP5]: Problem with mysql_fetch_object trying to convert toint???

Steve JORDI wrote:

> while( ($this->row = $this->db->getRow() ) != 0 )


This is implicitly trying to cast $this->row (which is an object) into an
integer, so that it can be compared to 0.

In this case, you probably want:

while ($this->row = $this->db->getRow())

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 27 days, 22:15.]

Bottled Water
http://tobyinkster.co.uk/blog/2008/02/18/bottled-water/
Reply With Quote
  #4 (permalink)  
Old 02-27-2008
Steve JORDI
 
Posts: n/a
Default Re: -> [PHP5]: Problem with mysql_fetch_object trying to convert to int???


Jerry, Toby,

Thanks a lot for your help.

What can I say besides "Shame on me! This one was really simple!"

It did the trick and now everything's back to normal.

Looks like PHP4 was less compliant with type casting.


Sincerely,
Steve JORDI

(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:55 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0