Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if $row['column_name'] is returning empty php mysql

Tags:

php

mysql

I have a table with columns

id,name,phone,describe

While fetching the values from this table i am using

$row=mysql_fetch_array($query) 

Now i want to check whether

$row['describe'] 

is returning empty value. How to check in php??

like image 887
Varuni N R Avatar asked Feb 18 '16 06:02

Varuni N R


People also ask

How do you check if SQL result is empty in PHP?

The easiest way is to use mysql_num_rows(). $cnt = mysql_num_rows($Result); if ( 0===$cnt ) { echo 'no records'; } else { while( false!=( $row=mysql_fetch_array($Result)) ) { // ... } }

How do I check if a field is empty in SQL?

SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR yourSpecificColumnName = ' '; The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value.

How do you check database field is empty or not PHP?

You can use the empty() function which will check to see if anything is there. The isset() checks for a null (boolean==false) on the field - which you can also use.

Is null in php mysql?

A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value. Note: A NULL value is different from a zero value or a field that contains spaces.


2 Answers

You can use ==0, this will check if it equals to 0:

if ($row['describe']==0) { /* code to do */ }

Or empty(), this will check if it is empty:

if (empty($row['describe'])) { /* code to do */ }

Personally, I would prefer !empty() as this will check if the variable is empty.

Hope this helps, thanks!

like image 179
Panda Avatar answered Nov 14 '22 22:11

Panda


Try this :

if(empty($row['describe'])) {
    //$row['describe'] is empty
} else {
    //$row['describe'] is not empty
}

Also please dont use mysql_*. It's deprecated and removed from PHP 7. Use mysqli_* or PDO.

like image 29
Mr. Engineer Avatar answered Nov 14 '22 23:11

Mr. Engineer