• 0

PHP Help: if statement


Question

Ok.. I have a SQL database with a partially filled out table. I am writing an If statement to show the result only if the variable or result is true. I am pulling my hair out because when I put in a result that is NULL, it still thinks its true.

Code:

$id = $_GET['id'];

$b = "SELECT * FROM mydatabase WHERE id = '$id'";

$b_query = mssql_query($b, $connect) or die(mssql_error());

$bp3=mssql_result($b_query,0,"picture3");

<?php if ($bp3) { ?>

<img src="../uploads/<?php echo $bp3; ?>">

<?php } ?>

---------------------------

Now, i know for a fact that bp3 = nothing. When I put another entry in place of picture3 (sus_misc), it actually works! But any of the "picture" fields seem to not work. I also have this same statement for at least 8 other table entries which all work fine as well.

All of the rows generally have the same data type and allow nulls.

If I type it *** if ($bp == false) it works.. so for some reason it thinks there is something there.. and I dont know why.

Any ideas?

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

after much searching.. issue seems to be connected to a bug in either the microsoft libraries or php itself. in an empty field var_dump returns '' which is the same as ' ' (or a space). If I add the line:

if ($myvar != ' ') it complies. if I do the opposite with == then it actually shows the context behind the echo...

The confusing thing is that I am running version 5+ which someone mentioned in the bug sheet was fixed.

http://bugs.php.net/bug.php?id=26315

Link to comment
Share on other sites

  • 0

If you want to be sure, you could just use trim() to make sure any whitespace at the beginning or at the end is removed before you check whether it's empty:

&lt;?php if ( trim($bp3)  != '' ) { ?&gt;
&lt;img src="../uploads/&lt;?php echo $bp3; ?&gt;"&gt;
&lt;?php } ?&gt;

However I agree that if this is indeed caused by a bug in PHP or MSSQL, it's a pretty nasty one. :p

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.