• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

PHP Help: if statement

Question

Volatile    13

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?

Share this post


Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0
Volatile    13

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

Share this post


Link to post
Share on other sites
  • 0
Calculator    22

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

Share this post


Link to post
Share on other sites
  • 0
Hot    226

if (!empty($bp3)) {

Problem solved.

If it isn't, then your field is not actually NULL.

Share this post


Link to post
Share on other sites
  • 0
Tekkerson    65

^+1 To that.

Share this post


Link to post
Share on other sites
  • 0
Kami-    178

Your SELECT statement should pull specific fields, not a *, then use a function wrapper around the picture3 field to replace NULL with FALSE.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.