Kelsey
Kelsey

Reputation: 931

How to Check if Column is NULL Instead of Empty Using PHP and MSSQL

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.

Upvotes: 0

Views: 2659

Answers (3)

Deadlock
Deadlock

Reputation: 1577

Update

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

     else {
        $picature2=stripslashes($array1['photo1']);
    }

Upvotes: 1

Class
Class

Reputation: 3160

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']);
}

Upvotes: 0

SajithNair
SajithNair

Reputation: 3867

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

Upvotes: 0

Related Questions