Gopal1216
Gopal1216

Reputation: 437

how to retrieve values from database that are not null

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!!

Upvotes: 0

Views: 1211

Answers (3)

underscore
underscore

Reputation: 6887

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

Upvotes: 2

Xtian
Xtian

Reputation: 498

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

Upvotes: 1

ndee
ndee

Reputation: 59

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.

Upvotes: 0

Related Questions