PHP - IF-statement false, still runs - php

The last hour I've been sitting with this problem. I have two if-statements (for testing purposes they are both IF-statements, and not IF- and ELSE IF-statements. The code runs the false IF-statement as if it is true.
The code:
<?php
$sth = $pdo->query("SELECT * FROM myDBTable WHERE alien1='$idkod' OR alien2='$idkod'");
$result = $sth->fetchAll();
if(!$result)
{
echo "No data";
}
else
{
foreach($result as $row)
{
$alien1 = $row['alien1'];
$alien2 = $row['alien2'];
if($idkod == $alien1)
{
echo $idkod . "==" . $alien1;
}
if($idkod == $alien2)
{
echo $idkod . "==" . $alien2;
}
}
}
?>
This will give me the following text on screen:
1234567891234567891234567==1234567891234567891234567
1234567891234567891234567==1234567891234567891234568
Clearly, the second text shouldn't be there, as the statement is not true.

Don't assume anything when making conditional forks, use var_dump() on the variables to temporarily look inside them - that way you best decide how to check for the exact type and value you are expecting.
Then as said already, prefer to check using ===
If you adopt this behaviour you will save countless hours and avoid some quite subtle bugs which can appear in your code.
Having the PHP Truth Tables pinned up for a while will help.

== ignores type when testing for equality. In this case it will assume that both strings are numbers and convert them. This means this will turn into:
9223372036854775807 == 9223372036854775807 //Max int val. Will be different on different systems.
=== will make sure that both arguments are the same type and will not attempt to coerce making
'1234567891234567891234567' === '1234567891234567891234568';
Give the expected result.
PHP equality is wacky sometimes.

It is wrong to use == you need to use === the second is value comparison, the first is object comparison (depending on the context)

I have modified your code a little check it
<?php
$sth = $pdo->query("SELECT * FROM myDBTable WHERE alien1='$idkod' OR alien2='$idkod'");
$result = $sth->fetchAll();
if(!$result)
{
echo "No data";
}
else
{
foreach($result as $row)
{
$alien1 = $row['alien1'];
$alien2 = $row['alien2'];
if($idkod == $alien1 && $idkod != $alien2)
{
echo $idkod . "==" . $alien1;
}
if($idkod == $alien2 && $idkod != $alien1)
{
echo $idkod . "==" . $alien2;
}
}
}
?>

Related

Why is my if statement not working the way I expect?

I am trying to achieve the following: I ask my SQL database a query using SELECT * FROM subjects. After doing that I ask for the array using mysqli_fetch_assoc. Until that point all is fine. The problem now is that when I try to modify in each loop the value of $genero depending if it's 1 or 0. But the value of $genero never changes, it's always 1 and I am sure that the array is fetching 0 and 1. Any idea while the values of $genero are not changing through the loop?
while ($subject = mysqli_fetch_assoc($result)) {
if ($subject["sexo"] = 1) {
$genero = "<img src='images/hombre.png' />";
} else {
$genero = "<img src='images/mujer.png' />";
}
echo $genero;
}
Your comparison operator is wrong. You're using = which is an assignment operator. In your example it will always be true. You need to use == which is a comparison operator.
if ($subject["sexo"] = 1) {
should be
if ($subject["sexo"] == 1) {
In if statement you should use double equal sign: if (a == b)

PHP/MySQL Checking for null

$sql = 'SELECT * FROM `phpbb_profile_fields_data`';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
if ($row['pf_kp_em_no_bonethr'] == '1') {
echo " Was 1";
} else if ($row['pf_kp_em_no_bonethr'] == '2') {
echo "Was 2";
} else {
echo "Was Neither 1 or 2";
}
}
$db->sql_freeresult($result);
I am curios, In my example I am checking the field for either a value of 1 or 2 but how do I check it for a value of NULL. Would it be any of the following three:
if ($row['pf_kp_em_no_bonethr'] == '')
if ($row['pf_kp_em_no_bonethr'] == '-1')
if ($row['pf_kp_em_no_bonethr'] == 'NULL')
Normally I would just try it out but I am not at home and wont be for the foreseeable future it has been bugging me. I am pretty sure it's not the second but I have seen -1 used for a null value in other languages. So can someone verify how I would indeed check for a NULL value please.
if ($row['pf_kp_em_no_bonethr'] === NULL)
Something like this should work.
if (is_null($row['pf_kp_em_no_bonethr'])) {
echo "Is NULL";
}
MySQL will return NULL values to PHP as actual PHP NULL. In this situation, what you need is:
// Notice lack of quotes around NULL
// And use === to distinguish type properly between integer 0 and NULL
if ($row['pf_kp_em_no_bonethr'] === NULL)
However, it would be more appropriate to check it in the query if NULL values are what you need to work with in PHP.
$sql = 'SELECT * FROM `phpbb_profile_fields_data` WHERE pf_kp_em_no_bonethr IS NULL';
Or to find all three values:
$sql = 'SELECT * FROM `phpbb_profile_fields_data`
WHERE pf_kp_em_no_bonethr IS NULL
OR pf_kp_em_no_bonethr IN (1,2)
';
I'd recommend to be very carfull with this one: I have seen
<?php
$field=$row['fieldname'];
if ($field===null) {
//Do something
}
?>
fail intermittently, especially on windows. This is why I prefer
SELECT
IFNULL(fieldname,'some_safe_value') AS fieldname
...
FROM
...
and the resulting trivial null-check.
Use is_null or === NULL.
if(is_null($row['pf_kp_em_no_bonethr'])){
}
or
if($row['pf_kp_em_no_bonethr'] === NULL){
}

goto equivalent for php version < 5.3.0?

I need to use the goto operator in my code as I can't seem to think of a way around it. However the problem is my host only has PHP version 5.2.17 installed.
Any ideas?
Below is my code:
if ($ready !=="y")
{
$check=mysql_query("SELECT `inserted` FROM `team`");
$numrows = mysql_num_rows($check);
$i="0";
while ($i<$numrows && $row = mysql_fetch_assoc($check))
{
$array[$i] = $row['inserted'];
$i++;
}
if (in_array("n", $array))
{
goto skip;
}
else
{
mysql_query("
UPDATE game SET ready='y'
");
}
}
skip:
There are a few anti-patterns in your code. Let's clean it up. I'll explain what's been changed in a jiffy.
if($ready !== "y") {
$sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
if(mysql_num_rows($sth) > 0) {
mysql_query("UPDATE game SET ready = 'y'");
}
}
First things first: There is no need to perform a query, fetch all of the results, then loop through those results (in_array) looking for a specific value. Let the database do that for you by expressly looking only for rows where inserted is the string literal "n".
Because we know that we're only getting "n" records back, we just need to check if there are any results. If so, run the query. If there are no "n" records, the UPDATE isn't run.
If you need to know that the UPDATE ran, add a check for it:
$ran_update = false;
if($ready !== "y") {
$sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
if(mysql_num_rows($sth) > 0) {
mysql_query("UPDATE game SET ready = 'y'");
$ran_update = true;
}
}
if($ran_update) {
// ...
}
You want to use the correct control word to break from the loop:
if ($ready !=="y")
{
$check=mysql_query("SELECT `inserted` FROM `team`");
$numrows = mysql_num_rows($check);
$i="0";
while ($i<$numrows && $row = mysql_fetch_assoc($check))
{
$array[$i] = $row['inserted'];
$i++;
}
if (in_array("n", $array))
{
break;
}
else
{
mysql_query("
UPDATE game SET ready='y'
");
}
}
The break keyword will do exactly what you want: End the execution of the while loop. Never ever ever use a goto!
As a direct response to the title of this post:
do{
if (!$condition){
break;
}
// Code called if conditions above are met
}while(0);
//Code always called
In some circumstances, this, or a goto, can make for very tidy and readable code.
First, you could use a break to exit your initial loop. Second, if you need to test for anything, set a variable (must be global not local) as a flag or indicator before calling break, then do a conditional test statement where your skip line is to perform any additional steps you need.

how to echo the line one time under the while loop

I have a code like this
<?php
$getLeftSide = 'select * from leftmenu';
$result = $db -> query ($getLeftSide) or die ("$db->error");
if ($result) {
while ($row = $result -> fetch_object()) {
$getCat = $row -> left_item_cat;
if ($getCat == 1) {
echo "<div class='left_main_cat'>Web and Desigen</div>";
echo "<a href='index.php?learn_id= $row->left_item_link'><div class='left_label_sub'>$row->left_item_name</div></a>";
}
}
}
?>
I need to echo this line one time
echo "<div class='left_main_cat'>Web and Design</div>";
of course it's under the while loop so it print it self many times
is there is a why to solve this and print this line one time only.
As Ben suggests, the easiest and clearest solution is to use a boolean check variable:
$catFound = FALSE;
while ($row = $result -> fetch_object()) {
$getCat = $row -> left_item_cat;
if ($getCat == 1) {
// We only want this category printed for the first category,
// if it exists.
if(!$catFound) {
echo "<div class='left_main_cat'>Web and Desigen</div>";
$catFound = TRUE;
}
echo "<a href='index.php?learn_id= $row->left_item_link'><div class='left_label_sub'>$row->left_item_name</div></a>";
}
}
Although it seems better to use a boolean variable and a comment to make clear your intent.
Ugly, but I'm not that good at PHP yet!
$has_printed = FALSE;
while ($row = $result -> fetch_object()) {
$getCat = $row -> left_item_cat;
if ($getCat == 1) {
if(!$has_printed){
echo "<div class='left_main_cat'>Web and Desigen</div>";
$has_printed = TRUE;
}
echo "<a href='index.php?learn_id= $row->left_item_link'><div class='left_label_sub'>$row->left_item_name</div></a>";
}
}
Simple solution: Add another boolean variable with default value false Then just check if false, print your text and set it true.
adding a boolean or check variable or any kind of conditional inside of a loop is a very bad idea imho, i NEVER do it. every time you swing through the loop, the condition has to be checked. it's like a badly written switch/case.
figure out a way to do it before the loop.
unroll the loops, most people do it to save space but most compilers will unroll them anyway for optimization
stick it before the loop
find a java or css way to hide it until the loop is done or you want to display stuff.

Why does using an If statement change a variable?

I am reading a text file and processing some records, a relevant sample of the text file is
#export_dategenre_idapplication_idis_primary
#primaryKey:genre_idapplication_id
#dbTypes:BIGINTINTEGERINTEGERBOOLEAN
#exportMode:FULL
127667880285760063715151750
127667880285760123715151751
I want to perform a specific action when application_id is already stored within my database AND is_primary = 1
I wrote this PHP to test my code:
$fp1 = fopen('genre_application','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}
while (!feof($fp1)) {
$line = stream_get_line($fp1,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue; //Skip lines that start with #
$field = explode ($delimiter, $line);
list($export_date, $genre_id, $application_id, $is_primary ) = explode($delimiter, $line);
// does application_id exist?
$application_id = mysql_real_escape_string($application_id);
$query = "SELECT * FROM jos_mt_links WHERE link_id='$application_id';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0 ) {
echo $application_id . "application id has genre_id" . $genre_id . "with primary of " . $is_primary. "\n";
} else
{
// no, application_id doesn't exist
}
} //close reading of genre_application file
fclose($fp1);
which results in this output on screen and is exactly as I expected.
371515175application id has genre_id6006with primary of 0
371515175application id has genre_id6012with primary of 1
If I then add an IF statement as in the code below, it somehow changes the value of is_primary as shown by the screen display
$fp1 = fopen('genre_application','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}
while (!feof($fp1)) {
$line = stream_get_line($fp1,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue; //Skip lines that start with #
$field = explode ($delimiter, $line);
list($export_date, $genre_id, $application_id, $is_primary ) = explode($delimiter, $line);
// does application_id exist?
$application_id = mysql_real_escape_string($application_id);
$query = "SELECT * FROM jos_mt_links WHERE link_id='$application_id';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0 ) {
if ($is_primary = '1') echo $application_id . "application id has genre_id" . $genre_id . "with primary of " . $is_primary. "\n";
} else
{
// no, application_id doesn't exist
}
} //close reading of genre_application file
fclose($fp1);
?>
The code above results in the following screen display, which incorrectly has the first field with a primary of 1, when as can be seen by the previous screen display and the sample text file it should be 0
371515175application id has genre_id6006with primary of 1
371515175application id has genre_id6012with primary of 1
Can anyone explain what I am doing to make the variable change and how I should use the If correctly please?
You are assigning a value instead of comparing:
($is_primary = '1')
you need
($is_primary == '1')
or === for a type-safe comparison.
This is why some people like to write their comparisons like so:
('1' == $is_primary)
the mistake is impossible to make here because "1" can't be assigned anything.
Personally though, I think that over time and with growing practice, one will learn to spot the mistake.
You need to use:
if ($is_primary == '1')
NOT
if ($is_primary = '1')
Because "=" defines variable and returns true, but "==" actually compares and doesn't change anything.
The if-line has only one = (assign value) instead of two == (compare value).
Try this instead:
if ($is_primary == '1')
if ($is_primary = '1')
You need to use the comparison operator ==, not the assignment operator =
if ($is_primary == '1')
....
NOT = BUT ==
Also consider removing all the stuff that bug the reading of your question for example somthing like:
// does application_id exist?
$res = mysql_query($query);
if (mysql_num_rows($res) > 0 ) {
echo $is_primary. "\n";
}
This is more readable and fit you purpose.
You may even find yourself does kind of bug.
If you type
if($x=10)...
Then you're telling it to attempt the operation
$x=10
to which it will return a success (return true). This will always run the loop, and alter the value of x.
To test $x, use
if($x==10)...
But note that to test for $x not equals 10 is
if($x!=10)...
ie, one equals and one bang.
Chek out PHP comparison operators

Categories