-2

Basically, I am wanting to detect if a database column is empty or if it contains text. The text doesn't have to be a specific set text just text.

If the column is empty, then show a message in php.

I have tried to use the row in question but still learning php so not quite sure the way around this.

if ($row['insert_time'] contains "") then {echo "The row contains no text"};

If the column insert_time contains no text, I want it to display "No Results" or something to let the user know there's no text.

If the column insert_time contains text, then it will display the rest of the function I have found useful in my case.

I am connecting to the database using the following;

```

$link = mysqli_connect("localhost", "root", "", "chaotic");
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error() . " Please enter correct details in the install/config file.");
}

?> ```

The insert_time is however set for date and time, but with the database export file I was given to test with, contains 2 entries with no insert_time. So in future reference instead of PHP displaying it as an empty table row, I want to let user's know that the status is Unknown or similar.

What I also forgot to mention, is I am using a script from the web for "Facebook Time Ago" function, Its setup correctly but if the empty database column contains nothing, it automatically sets the table row to 46 Years.

WK8YOU
  • 11
  • 2
  • 1
    What is the MySQL type of the `insert_time` column? If it be a date and you have it set to be not nullable, then it can never be empty. – Tim Biegeleisen Dec 26 '18 at 08:11
  • 1
    How are you connecting to the DB? mysqli? pdo? using another abstraction layer as doctrine? write the code you are using for you DB query within your question. – yivi Dec 26 '18 at 08:12
  • What do you use to disconnect/close the connection - or do you leave it hanging with resources in use? – Wilson Hauck Dec 26 '18 at 13:09

2 Answers2

1

You want to check condtion with php like

if (isset($row['insert_time']) && $row['insert_time'] != ''){ 
    echo $row['insert_time']; 
}else{
    echo "The row contains no text";
};
Ravi Chauhan
  • 1,409
  • 13
  • 26
1
  1. Since values returned from the database are always strings (except if they are NULL), you can check if the length of the value is 0 or more characters.

  2. And because of PHP's weak typing an if statement like this...

    if ($row['insert_time'] == "") { }

    Will actually become true even when $row['insert_time'] contains the value of 0 (AKA non empty).

So it is best to perform your evaluation based on the first point above (checking the length of the value). Like so...

if (strlen($row['insert_time']) > 0) { 
    echo $row['insert_time']; 
}
else {
    echo "No Results";
};
  • 1
    The issue of typing is one that can be solved... https://stackoverflow.com/a/25692758/362536 I was shocked to see that this is still an issue by default. Haven't had to deal with it in many years. :-) – Brad Dec 27 '18 at 06:50