【问题标题】:How to Check if Column is NULL Instead of Empty Using PHP and MSSQL如何使用 PHP 和 MSSQL 检查列是否为空而不是空
【发布时间】:2014-02-25 03:59:31
【问题描述】:

我的 PHP 代码应该显示保存到我的 MSSQL 表的行列 photo 中的图像名称。有些行将photo 列设置为NULL,而有些行在photo 列中有数据02.jpg。如果我使用下面的代码,02.jpg 不会出现,因为它以零开头。我正在寻找一种方法来检查该列是否设置为 NULL 而不是空的,因为空不允许某些图像名称。

这是 PHP 代码:

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

如果photo 列设置为NULL,上面的代码正确显示“无图像”,但如果photo 列是02lady.jpg,则它将不起作用,因为它以零开头。

这是我尝试过的:

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

到目前为止,如果photo 的列数据为 NULL,则该方法不起作用并且不会显示“无图像”。

感谢您的帮助。非常感谢所有帮助。

【问题讨论】:

  • 试试$picature2['photo1']===null
  • 尝试“is_null”或“isset”:in3.php.net/is_null
  • 它以0 开头,那又怎样? '02.jpg' 不会被视为空。
  • 您正在检查$picature2,然后使用$array1。你的代码能以某种方式工作吗?
  • 它确实被视为空的。我使用的是旧版本的 MSSQL,所以也许这就是原因。是的,我的代码确实有效。

标签: php sql sql-server


【解决方案1】:

更新

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

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

【讨论】:

  • 我尝试使用它,但它仍然没有返回“无图像”值。我有一个删除按钮将photo 列重置为 NULL,而 sql 是这样的:$sql = "UPDATE used_trailers1 SET photo1 = NULL WHERE orderid = '$sn'"; $retval2 = mssql_query($sql, $dbLink); is my $sql not setting the column to NULL?
  • echo gettype($picature2['photo1']) 的输出是什么?
  • 输出为string
  • 所以它不是NULL。尝试trim 并再次检查其类型。
  • trim 到底是什么意思?
【解决方案2】:

可以使用php is_null()函数http://us2.php.net/manual/en/function.is-null.php

【讨论】:

    【解决方案3】:

    你有以下

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

    photo1 分配给$picature2 然后检查$picature2['photo1'] 哪个$picature2 不是数组。

    您应该使用以下内容

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

    【讨论】:

    • 为什么要跨层展开逻辑?你甚至可以在查询中生成结果html,但这没有任何意义。
    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    相关资源
    最近更新 更多