0

I'm trying to retrieve values from my table that are not null. Below is the picture of my table forumposts.

forumposts table

I want to retrieve all the thread titles that are not null. I'm using the following code :

$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 />";
    }
}

Null values are still coming. Please help!!

7
  • what is your thread_title TYPE ? Commented Mar 23, 2014 at 13:10
  • 1
    It should be thread_title IS NOT NULL AND thread_title<>'' Commented Mar 23, 2014 at 13:11
  • NULL is not the same as 'blank' or 'empty' or 'space'. GIGO Commented Mar 23, 2014 at 13:12
  • my thread_title type is varchar(255). Commented Mar 23, 2014 at 13:12
  • stackoverflow.com/questions/1869264/… Commented Mar 23, 2014 at 13:13

3 Answers 3

2

Make your thread_title is

varchar(255) and Default value is NULL 

and USe your query

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

OUTPUT

THREAD_TITLE

foo
bar

HERE IS THE WORKING SQL FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

1

Should be like this:

$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 />";
    }
}

Comments

0

You can just change it into:

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

Which will select anything that is different than an empty string.

Comments

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.