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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 ------------------------------------------------ |
|
|||
|
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 ================== |
|
|||
|
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/ |
|
|||
|
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 ------------------------------------------------ |