Broken IF behavior? (Changing the branch changes the evaluation)
Broken IF behavior? (Changing the branch changes the evaluation)
am 28.07.2009 20:48:44 von Matt Neimeyer
Background: I'm converting a webapp from Visual FoxPro as a backend to
MySQL... However one part of our app (a system status checker) is
common code between the versions.
I've got the following function... In English (in case it's not
apparent), if the version of the app is 2.0 or higher, then use MySQL
functions, otherwise use the ODBTP function to connect to VFP.
function my_fetch_array($result)
{
global $Version;
if(version_compare($Version,"2.0",">="))
{ $Ret = mysql_fetch_array($result); if(!$Ret) { } else { return $Ret; } }
else { $Ret = odbtp_fetch_array($result); if(!$Ret) { } else { return $Ret; } }
}
This feels like a hack but works perfectly. Data is returned and all
is right with the world. Until I added in extra "error reporting".
When I change the if(!$Ret) portion as such...
if(!$Ret) { die("myError".mysql_error()); } else { return $Ret; }
It ALWAYS dies... and I see "myError" on the screen... If I change it
like such...
if(!$Ret) { } else { echo "notError"; return $Ret; }
I always see the "notError" on the screen and $Ret gets returned.
WHY does adding the die() inside the { } change the way the if is evaluated?
By the way I've tested this on 4.4.x on OSX and Windows, and on 5.2.5
on Windows...
Thanks
Matt
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Broken IF behavior? (Changing the branch changes the
am 28.07.2009 20:56:48 von Eddie Drapkin
On Tue, Jul 28, 2009 at 2:48 PM, Matt Neimeyer wrote:
> Background: I'm converting a webapp from Visual FoxPro as a backend to
> MySQL... However one part of our app (a system status checker) is
> common code between the versions.
>
> I've got the following function... In English (in case it's not
> apparent), if the version of the app is 2.0 or higher, then use MySQL
> functions, otherwise use the ODBTP function to connect to VFP.
>
> function my_fetch_array($result)
> Â Â Â Â {
> Â Â Â Â global $Version;
> Â Â Â Â if(version_compare($Version,"2.0",">=3D"))
> Â Â Â Â Â Â Â Â { $Ret =3D mysql_f=
etch_array($result); if(!$Ret) { } else { return $Ret; } }
>     else   { $Ret =3D odbtp_fetch_array=
($result); if(!$Ret) { } else { return $Ret; } }
> Â Â Â Â }
>
> This feels like a hack but works perfectly. Data is returned and all
> is right with the world. Until I added in extra "error reporting".
> When I change the if(!$Ret) portion as such...
>
> Â Â Â Â if(!$Ret) { die("myError".mysql_error()); } el=
se { return $Ret; }
>
> It ALWAYS dies... and I see "myError" on the screen... If I change it
> like such...
>
> Â Â Â Â if(!$Ret) { } else { echo "notError"; return $=
Ret; }
>
> I always see the "notError" on the screen and $Ret gets returned.
>
> WHY does adding the die() inside the { } change the way the if is evaluat=
ed?
>
> By the way I've tested this on 4.4.x on OSX and Windows, and on 5.2.5
> on Windows...
>
> Thanks
>
> Matt
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
What's the output of var_dump($Ret) right before that if statement?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Broken IF behavior? (Changing the branch changes the
am 28.07.2009 21:03:27 von Matt Neimeyer
It's exactly what I would expect... The content of the row... But in
any case, what does changing the content of the { } branch have to do
with how the IF() itself is evaluated?
array(4) {
[0]=3D>
string(8) "CustName"
["config"]=3D>
string(8) "CustName"
[1]=3D>
string(11) "Sample Cust"
["value"]=3D>
string(11) "Sample Cust"
}
On Tue, Jul 28, 2009 at 2:56 PM, Eddie Drapkin wrote:
> On Tue, Jul 28, 2009 at 2:48 PM, Matt Neimeyer wrote:
>> Background: I'm converting a webapp from Visual FoxPro as a backend to
>> MySQL... However one part of our app (a system status checker) is
>> common code between the versions.
>>
>> I've got the following function... In English (in case it's not
>> apparent), if the version of the app is 2.0 or higher, then use MySQL
>> functions, otherwise use the ODBTP function to connect to VFP.
>>
>> function my_fetch_array($result)
>> =A0 =A0 =A0 =A0{
>> =A0 =A0 =A0 =A0global $Version;
>> =A0 =A0 =A0 =A0if(version_compare($Version,"2.0",">=3D"))
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{ $Ret =3D mysql_fetch_array($result); if=
(!$Ret) { } else { return $Ret; } }
>> =A0 =A0 =A0 =A0else =A0 =A0{ $Ret =3D odbtp_fetch_array($result); if(!$R=
et) { } else { return $Ret; } }
>> =A0 =A0 =A0 =A0}
>>
>> This feels like a hack but works perfectly. Data is returned and all
>> is right with the world. Until I added in extra "error reporting".
>> When I change the if(!$Ret) portion as such...
>>
>> =A0 =A0 =A0 =A0if(!$Ret) { die("myError".mysql_error()); } else { return=
$Ret; }
>>
>> It ALWAYS dies... and I see "myError" on the screen... If I change it
>> like such...
>>
>> =A0 =A0 =A0 =A0if(!$Ret) { } else { echo "notError"; return $Ret; }
>>
>> I always see the "notError" on the screen and $Ret gets returned.
>>
>> WHY does adding the die() inside the { } change the way the if is evalua=
ted?
>>
>> By the way I've tested this on 4.4.x on OSX and Windows, and on 5.2.5
>> on Windows...
>>
>> Thanks
>>
>> Matt
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> What's the output of var_dump($Ret) right before that if statement?
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Broken IF behavior? (Changing the branch changes the evaluation)
am 28.07.2009 22:22:01 von Shawn McKenzie
Matt Neimeyer wrote:
> Background: I'm converting a webapp from Visual FoxPro as a backend to
> MySQL... However one part of our app (a system status checker) is
> common code between the versions.
>
> I've got the following function... In English (in case it's not
> apparent), if the version of the app is 2.0 or higher, then use MySQL
> functions, otherwise use the ODBTP function to connect to VFP.
>
> function my_fetch_array($result)
> {
> global $Version;
> if(version_compare($Version,"2.0",">="))
> { $Ret = mysql_fetch_array($result); if(!$Ret) { } else { return $Ret; } }
> else { $Ret = odbtp_fetch_array($result); if(!$Ret) { } else { return $Ret; } }
> }
>
> This feels like a hack but works perfectly. Data is returned and all
> is right with the world. Until I added in extra "error reporting".
> When I change the if(!$Ret) portion as such...
>
> if(!$Ret) { die("myError".mysql_error()); } else { return $Ret; }
>
> It ALWAYS dies... and I see "myError" on the screen... If I change it
> like such...
>
> if(!$Ret) { } else { echo "notError"; return $Ret; }
>
> I always see the "notError" on the screen and $Ret gets returned.
>
> WHY does adding the die() inside the { } change the way the if is evaluated?
>
> By the way I've tested this on 4.4.x on OSX and Windows, and on 5.2.5
> on Windows...
>
> Thanks
>
> Matt
I'm assuming that you are calling my_fetch_array() in a loop of some
sort and so at some point there are no more records in the result.
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Broken IF behavior? (Changing the branch changes the evaluation)
am 29.07.2009 10:12:55 von Peter Ford
Matt Neimeyer wrote:
> It's exactly what I would expect... The content of the row... But in
> any case, what does changing the content of the { } branch have to do
> with how the IF() itself is evaluated?
>
> array(4) {
> [0]=>
> string(8) "CustName"
> ["config"]=>
> string(8) "CustName"
> [1]=>
> string(11) "Sample Cust"
> ["value"]=>
> string(11) "Sample Cust"
> }
>
>
The if() *is* being evaluated *the same* whatever the content of that branch,
but when there's no content, you see no result...
It always looks odd to me to have empty if branches - why do you not just write
if ($Ret) { return $Ret; }
Anyway, the !$Ret branch is being executed because the fetch operation will
return NULL (or FALSE or something equivalent) when there are no results.
--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Broken IF behavior? (Changing the branch changes the
am 29.07.2009 17:47:15 von Matt Neimeyer
>> $Ret = mysql_fetch_array($result); if(!$Ret) { } else { return $Ret; }
> I'm assuming that you are calling my_fetch_array() in a loop of some
> sort and so at some point there are no more records in the result.
Oh... Um... Yeah... Well...
So... Checking the docs... "Returns an array of strings that
corresponds to the fetched row, or FALSE if there are no more rows."
Is there a way to differentiate between a FALSE for no more rows and an error?
Matt
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Re: Broken IF behavior? (Changing the branch changes the evaluation)
am 29.07.2009 18:00:40 von M.Ford
> -----Original Message-----
> From: Matt Neimeyer [mailto:matt@neimeyer.org]
> Sent: 29 July 2009 16:47
>=20
> >> $Ret =3D mysql_fetch_array($result); if(!$Ret) { } else { return
> $Ret; }
> > I'm assuming that you are calling my_fetch_array() in a loop of
> some
> > sort and so at some point there are no more records in the result.
>=20
> Oh... Um... Yeah... Well...
>=20
> So... Checking the docs... "Returns an array of strings that
> corresponds to the fetched row, or FALSE if there are no more rows."
>=20
> Is there a way to differentiate between a FALSE for no more rows and
> an error?
I don't actually think that mysql_fetch_array can return an error -- read t=
hat description again, it's very clear that you get an array of fetched val=
ues or FALSE for no more data, no other options. If there's any error occur=
ring, it will be in the preceding mysql_query().
Cheers!
Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to http:=
//disclaimer.leedsmet.ac.uk/email.htm
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Broken IF behavior? (Changing the branch changes theevaluation)
am 29.07.2009 19:11:05 von Shawn McKenzie
Ford, Mike wrote:
>> -----Original Message-----
>> From: Matt Neimeyer [mailto:matt@neimeyer.org]
>> Sent: 29 July 2009 16:47
>>
>>>> $Ret = mysql_fetch_array($result); if(!$Ret) { } else { return
>> $Ret; }
>>> I'm assuming that you are calling my_fetch_array() in a loop of
>> some
>>> sort and so at some point there are no more records in the result.
>> Oh... Um... Yeah... Well...
>>
>> So... Checking the docs... "Returns an array of strings that
>> corresponds to the fetched row, or FALSE if there are no more rows."
>>
>> Is there a way to differentiate between a FALSE for no more rows and
>> an error?
>
> I don't actually think that mysql_fetch_array can return an error -- read that description again, it's very clear that you get an array of fetched values or FALSE for no more data, no other options. If there's any error occurring, it will be in the preceding mysql_query().
>
Probably the only error you can get is: supplied argument is not a valid
MySQL result resource, if $result is not valid. So you could check that
in the code that does the actual query.
As for the function, I would just do this:
function my_fetch_array($result)
{
if(version_compare($GLOBALS['Version'], "2.0", ">=")) {
return mysql_fetch_array($result);
} else {
return odbtp_fetch_array($result);
}
}
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php