Hi

I have been struggling with this for a while and cant find the answer, the code below shows my else if statement everything goes fine until using the else if statement and ' . $img . ' ends up being blank.

It should be if field 4 is 0.00 then ' . $img . ' should be nothing
else if field 4 is blank then ' . $img . ' should be nothing
else there is something in field 4 then ' . $img . ' should be <img src="/images/' . ($row['field4']) . '.jpg" height ="60" border="0" ALIGN="Left" />

All 3 come up with no code at all in the place where ' . $img . ' should be

Please help

$result = mysql_query("SELECT DISTINCT `field1`, `field2`, `field3`, `field4` FROM TABLE WHERE `field1` = '$txt1' and `field2` = '$txt2'") or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($result)){


                                if($row['field4'] == 0.00)
                        {
                            $img = '';
                        }
                        else
                        {
                            if($row['field4'] ='')
                            {

                                $img = '';

                            }
                            else
                            {

                                $osimg = '<img src="../'. $row['field4'].'" height ="150" border="0" ALIGN="Left" />';

                            }
                        }

echo '
                                  <img src="../'. $row['field3'].'" height ="150" border="0" ALIGN="Left" alt="'. $row['field1'].' '. $row['field2'].'" />' . $img . '';
}

?>

Dani AI

Generated

This was resolved by the two fixes called out by and : a variable-name typo and an accidental assignment in the conditional. Both made the image variable stay empty so nothing was printed.

Why that happens (short):

  • A single = inside an if assigns a value instead of comparing it. In a conditional that assignment is evaluated for truthiness, so it doesn’t behave like a comparison. See PHP comparison rules: Comparison Operators.
  • A second problem was assigning the image HTML to a different variable ($osimg) while the code echoed $img. Fix the typo so the same variable is set and echoed.

Practical tips to avoid similar issues:

  • Turn on full error reporting and dump the fetched value to verify what’s coming from the DB (undefined indexes, empty strings, unexpected types). That quickly shows if the field is blank, the string "0", or contains a path.
  • Use empty() or trim() to detect blanks, but note empty("0") is true — if you need to distinguish numeric zeros from empty strings, compare types or cast to a number first (see empty() for behavior).
  • Check the generated src path and the filesystem (relative vs absolute paths) if the image tag is present but nothing displays.

Security/maintenance note: the code shown uses the old mysql_* API which was removed in modern PHP; consider migrating to mysqli or PDO and use prepared statements. See PHP’s API guidance: Choosing an API.

Status: confirmed it’s sorted after those fixes.

Recommended Answers

All 6 Replies

On line 20, you have $osimg = ..., shouldn't this be $img = ...? From the code you currently have, $img will never be assigned a value if it field4 contains a value.

OK thanks, how would I change it so I do have a value please

Just change $osimg to $img on line 20 and give that a go.

No that has not worked

line 11 must have double equal to, comparison, u put only 1

if($row['field4'] ='')

make it

if($row['field4'] =='')

thanks, all sorted now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.