Jump to content

Display text if a database field is null else display another text


zazu

Recommended Posts

Hi guys,

 

I've a little problem i have a fileld in my database that i want to check. If is null to display some text else display another text. I've manage to do something like this but doesn't work. The code is below:

                    <?php
$result = @$mysqli->query("SELECT * FROM depanarecuora_clients WHERE paid_status = '$paid_status'");
while ($result = mysqli_fetch_assoc($result))
  if (is_null($paid_status)) {
  echo "some text";
} 
            ?>
Link to comment
Share on other sites

The whole piece of code is wrong.

 

First, do not use the @ to suppress errors. Errors are your friend. They tell you something is wrong and what the problem is.

 

If you just want the state of one column, just select that one column, not selecting everything with *.

 

By adding the WHERE condition you are specifying a certain status to return so you will not get all the status's of that column.

 

Then you are checking your user supplied value of $paid_status for null instead of the database column status.

 

Additionally, the query could be done with a MySQL IF

http://www.w3resource.com/mysql/control-flow-functions/if-function.php

 

SELECT 
IF(paid_status IS NULL,'Status is null','Status is not null') AS "paid_status"  
FROM depanarecuora_clients;  
Edited by benanamen
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.