zazu Posted November 26, 2015 Share Posted November 26, 2015 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/299587-display-text-if-a-database-field-is-null-else-display-another-text/ Share on other sites More sharing options...
benanamen Posted November 26, 2015 Share Posted November 26, 2015 (edited) 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 November 26, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/299587-display-text-if-a-database-field-is-null-else-display-another-text/#findComment-1527208 Share on other sites More sharing options...
Barand Posted November 26, 2015 Share Posted November 26, 2015 I am having difficulty seeing any connection between your query and the question Quote Link to comment https://forums.phpfreaks.com/topic/299587-display-text-if-a-database-field-is-null-else-display-another-text/#findComment-1527209 Share on other sites More sharing options...
zazu Posted November 26, 2015 Author Share Posted November 26, 2015 I am having difficulty seeing any connection between your query and the question If the the paid_status field from my data base is set to null display "some text". That is what i want to do. Quote Link to comment https://forums.phpfreaks.com/topic/299587-display-text-if-a-database-field-is-null-else-display-another-text/#findComment-1527210 Share on other sites More sharing options...
Barand Posted November 26, 2015 Share Posted November 26, 2015 (edited) SELECT IFNULL(paid_status, 'Some Text') as paid_status FROM depanarecuora_clients Edited November 26, 2015 by Barand add column alias Quote Link to comment https://forums.phpfreaks.com/topic/299587-display-text-if-a-database-field-is-null-else-display-another-text/#findComment-1527212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.