PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > -> [PHP5]: Problem with mysql_fetch_object trying to convert to int???
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
-> [PHP5]: Problem with mysql_fetch_object trying to convert to int???

Réponse
 
LinkBack Outils de la discussion
Vieux 26/02/2008, 14h55   #1
Steve JORDI
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut -> [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 .

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
------------------------------------------------
  Réponse avec citation
Vieux 26/02/2008, 15h39   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 .
>
> 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
==================

  Réponse avec citation
Vieux 26/02/2008, 15h59   #3
Toby A Inkster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/
  Réponse avec citation
Vieux 27/02/2008, 08h03   #4
Steve JORDI
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -> [PHP5]: Problem with mysql_fetch_object trying to convert to int???


Jerry, Toby,

Thanks a lot for your .

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
------------------------------------------------
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h45.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11753 seconds with 12 queries