0

I am showing a message to all my students using MySQL for which I am using this:

<?php
    while ($row_settings = mysql_fetch_array($rs_settings)) {
        echo $row_settings['message'];
    }
?>

But I want to show a message to user if the message field is blank.

Like so.

if !empty(message)
show (You have not subscribed any subject)
else show (message)

3 Answers 3

1

You can use conditon

<?php 
    while ($row_settings = mysql_fetch_array($rs_settings)) {
        if ($row_settings['message'] == '') {
            //your message
        } else {
            echo $row_settings['message']; 
        }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

This works

<?php 
while ($row_settings = mysql_fetch_array($rs_settings)) {
if ($row_settings['message'] == '')
{
   echo 'You have not subscribed any subject yet. Please use buy option to subscribe.';
}
else
{
    echo $row_settings['message']; 
}
} 

?>

Comments

0

I think you should use ternary operators and use the empty function anyway, unless the string from the database is likely to contain just '0'.

<?php 
while ($row_settings = mysql_fetch_array($rs_settings)) {
    echo !empty( $row_settings['message']) ?
       $row_settings['message'] :
       'You have not subscribed any subject yet. Please use buy option to subscribe.';
} 
?>

Also stop using mysql as mysql function calls are deprecated, not to mention a security risk. Use mysqli or PDO for your database interactions instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.