【问题标题】:how to retrieve values from database that are not null如何从数据库中检索非空值
【发布时间】:2014-03-23 13:09:52
【问题描述】:

我正在尝试从我的表中检索非空值。下面是我的表论坛帖子的图片。

我想检索所有不为空的线程标题。我正在使用以下代码:

$forum_activities = "";
$sql = "SELECT thread_title FROM forumposts WHERE thread_title IS NOT NULL";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows > 0)
{
    while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
    {
        $thread_titles = $row["thread_title"];
        $forum_activities .= "<a href='viewthread.php'>$thread_titles</a><hr />";
    }
}

仍会出现空值。请帮忙!!

【问题讨论】:

  • 你的 thread_title TYPE 是什么?
  • 应该是thread_title IS NOT NULL AND thread_title&lt;&gt;''
  • NULL 与“空白”或“空”或“空格”不同。 GIGO
  • 我的 thread_title 类型是 varchar(255)。

标签: php mysql


【解决方案1】:

让你的thread_title

varchar(255) and Default value is NULL 

并使用您的查询

CREATE TABLE IF NOT EXISTS `forumposts` (
  `thread_title` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `forumposts` (`thread_title`) VALUES
('foo'),
('bar'),
(NULL);

SELECT thread_title FROM forumposts WHERE thread_title IS NOT NULL

输出

THREAD_TITLE

foo
bar

HERE IS THE WORKING SQL FIDDLE

【讨论】:

    【解决方案2】:

    应该是这样的:

    $sql = "SELECT thread_title FROM forumposts WHERE thread_title"; //Remove the IS NOT NULL
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0)
    {
        while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
        {
            $thread_titles = $row["thread_title"];
            $forum_activities .= "<a href='viewthread.php'>$thread_titles</a><hr />";
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以改成:

      $sql = "SELECT thread_title FROM forumposts WHERE thread_title != ''";
      

      这将选择与空字符串不同的任何内容。

      【讨论】:

        猜你喜欢
        • 2018-11-25
        • 1970-01-01
        • 2017-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多