0

I have PHP Code that is supposed to display the image name that is saved into my MSSQL table's row column photo. Some rows have the photo column set to NULL while some have the data 02.jpg in the photo column. If i use the code below, 02.jpg does not show up because it starts with zero. I am looking to find a way to check if the column is set to NULL rather than empty, because empty is not allowing some image names.

Here is the PHP code:

$picature2 = "SELECT photo1 FROM used_trailers1 WHERE orderid = $sn";
$query1=mssql_query($picature2, $conn);
$array1=mssql_fetch_assoc($query1);
$picature2=stripslashes($array1['photo1']);

if (empty($picature2['photo1'])){
    $picature2= "No Image";
} else {
    $picature2=stripslashes($array1['photo1']);
}

The code above correctly displays the "No Image" if the photo column is set to NULL, but if the photo column is 02lady.jpg then it will not work because it starts with a zero.

Here is what I tried:

if (IS_NULL($picature2['photo1'])){

So far that does not work and does not display the "No Image" if the column data for photo is NULL.

Thanks for any help. All help is greatly appreciated.

9
  • 1
    try $picature2['photo1']===null Commented Feb 25, 2014 at 4:03
  • try 'is_null' or 'isset' : in3.php.net/is_null Commented Feb 25, 2014 at 4:04
  • 1
    It starts with 0 so what? '02.jpg' won't be treated as empty. Commented Feb 25, 2014 at 4:05
  • You're checking $picature2 then using $array1. Does your code work even somehow? Commented Feb 25, 2014 at 4:08
  • It does get treated as empty. I am using an older version of MSSQL so maybe that is why. Yes, my code does work, some how. Commented Feb 25, 2014 at 4:16

3 Answers 3

1

Update

  if ($picature2['photo1']===null || ctype_space($picature2['photo1'])){
    $picature2= "No Image";
    }

     else {
        $picature2=stripslashes($array1['photo1']);
    }
Sign up to request clarification or add additional context in comments.

9 Comments

I tried using this, but it still does not return the "No Image" value. I have a delete button to reset the photo column to NULL and the sql for that is this : $sql = "UPDATE used_trailers1 SET photo1 = NULL WHERE orderid = '$sn'"; $retval2 = mssql_query($sql, $dbLink); is my $sql not setting the column to NULL?
What's the output echo gettype($picature2['photo1']) of?
The output is string.
So its not NULL. Try to trim it and check its type again.
What exactly do you mean by trim?
|
0

You can use php is_null() function https://www.php.net/manual/en/function.is-null.php

Comments

0

You have the following

$picature2=stripslashes($array1['photo1']);
if (empty($picature2['photo1'])){

which assigns photo1 to $picature2 then you check $picature2['photo1'] which $picature2 isn't a array.

You should use the following

#$picature2=stripslashes($array1['photo1']);
#remove this because if you have NULL and use stripslashes it turns into ""
if($array1['photo1'] === NULL){
    $picature2 = "No Image";
}else{
    $picature2 = stripslashes($array1['photo1']);
}

1 Comment

Why to spread the logic across layers? You can even generate the result html in the query, but it makes no sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.