Problem with some codes

This is a discussion on Problem with some codes within the PHP General forums, part of the PHP Programming Forums category; Hello Can you help me please.. Here's some PHP code : // in a while process $value=""; $reqt = mysql_query(&...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-03-2006
Nico
 
Posts: n/a
Default Problem with some codes

Hello

Can you help me please..

Here's some PHP code :
// in a while process

$value="";

$reqt = mysql_query("select value from table where ref='$xxx'");

$value = mysql_result($reqt,0,"value");


$ok="0";

if ($value=="0"){

echo "$value";

$ok="1";

}

Normally only when value = 0 , ok = 1

but when i execute this script and when the sql doesn't return any fields

the ok=1 when value from sql = nothing (any fields returned)

i hope i'm clear :)




Reply With Quote
  #2 (permalink)  
Old 10-04-2006
Nico
 
Posts: n/a
Default Re: Problem with some codes

nobody can help me?

"Nico" <nico-bc@belcenter.com> a écrit dans le message de news:
45228ce7$1@ns2.belcenter.be...
> Hello
>
> Can you help me please..
>
> Here's some PHP code :
> // in a while process
>
> $value="";
>
> $reqt = mysql_query("select value from table where ref='$xxx'");
>
> $value = mysql_result($reqt,0,"value");
>
>
> $ok="0";
>
> if ($value=="0"){
>
> echo "$value";
>
> $ok="1";
>
> }
>
> Normally only when value = 0 , ok = 1
>
> but when i execute this script and when the sql doesn't return any fields
>
> the ok=1 when value from sql = nothing (any fields returned)
>
> i hope i'm clear :)
>
>
>
>



Reply With Quote
  #3 (permalink)  
Old 10-04-2006
mootmail-googlegroups@yahoo.com
 
Posts: n/a
Default Re: Problem with some codes

Nico wrote:
> Hello
>
> Can you help me please..
>
> Here's some PHP code :
> // in a while process
>
> $value="";
>
> $reqt = mysql_query("select value from table where ref='$xxx'");
>
> $value = mysql_result($reqt,0,"value");
>
>
> $ok="0";
>
> if ($value=="0"){
>
> echo "$value";
>
> $ok="1";
>
> }
>
> Normally only when value = 0 , ok = 1
>
> but when i execute this script and when the sql doesn't return any fields
>
> the ok=1 when value from sql = nothing (any fields returned)
>
> i hope i'm clear :)


This is happening because "0", is equivalent to FALSE and, I believe,
NULL, in PHP.
When your query returns no values, it is returning a value equivalent
to a 0.

There are two ways to try getting around this. The first, I'm not
certain will work, but you can try anyway.
- use '===' instead of '==' in your if statement. '===' tests for
equality and that the compared values are of the same type.
- check the number of rows returned with mysql_num_rows, then only echo
the variable if >0

Reply With Quote
  #4 (permalink)  
Old 10-05-2006
Nico
 
Posts: n/a
Default Re: Problem with some codes

i've found a workaround:
i used
if ($value != "" and $value=="0"){
}

works :)
because when sql doesn't return any values
$values = "" :-)

<mootmail-googlegroups@yahoo.com> a écrit dans le message de news:
1159993017.064199.264780@e3g2000cwe.googlegroups.c om...
> Nico wrote:
>> Hello
>>
>> Can you help me please..
>>
>> Here's some PHP code :
>> // in a while process
>>
>> $value="";
>>
>> $reqt = mysql_query("select value from table where ref='$xxx'");
>>
>> $value = mysql_result($reqt,0,"value");
>>
>>
>> $ok="0";
>>
>> if ($value=="0"){
>>
>> echo "$value";
>>
>> $ok="1";
>>
>> }
>>
>> Normally only when value = 0 , ok = 1
>>
>> but when i execute this script and when the sql doesn't return any fields
>>
>> the ok=1 when value from sql = nothing (any fields returned)
>>
>> i hope i'm clear :)

>
> This is happening because "0", is equivalent to FALSE and, I believe,
> NULL, in PHP.
> When your query returns no values, it is returning a value equivalent
> to a 0.
>
> There are two ways to try getting around this. The first, I'm not
> certain will work, but you can try anyway.
> - use '===' instead of '==' in your if statement. '===' tests for
> equality and that the compared values are of the same type.
> - check the number of rows returned with mysql_num_rows, then only echo
> the variable if >0
>



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 06:00 AM.


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