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(&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 :) |
|
|||
|
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 :) > > > > |
|
|||
|
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 |
|
|||
|
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 > |