How to Find Null Values in a MySQL Table Using PHP



PHP







In this article, we show how to find if there are any null values in a MySQL table using PHP.

Maybe you want to delete rows that have null values or insert values into rows that are null.

So this can have several application.

To do this in MySQL, you check to see whether a column is NULL. We show this below.



So the above is the MySQL code to select a table, named table_name, and check to see if column1 is NULL.

We'll now show how to tie this in with PHP.

PHP Code

The PHP code to return the rows of a table in any random order is shown below.



So the following code gets the data required to make a connection to a database and actually selects a database.

We then create a variable named $result that selects the table, table_name, and checks the column, column_name, to see if it is NULL.

So all you have to do is select a table and check to see if a particular column is NULL.

The next block of code, consisting of a while loop, loops through all the rows and retrieves those rows which contains a NULL value for the column_name column. One of the columns is name. So we echo out the name that that has a NULL value for column_name.

Example

So now we show an actual example of a MySQL table and we will echo out the rows that are NULL.

Below is a MySQL table representing a customer information, customers' names and emails.

MySQL Orders table

I use the same PHP code as shown above, only specific to this table. The output of this is shown below.

Actual PHP Output


Fred
Ryan



How to Find NULL Values in Multiple Columns

So the above code showed you how to find a row in which a single column is null.

But what if you want to find out if any columns or a combination of columns have null values.

MySQL OR Condition

To find out if any of columns of those you list are null, the following PHP code can be used, shown below.



If any of these columns have null values, the row will be returned.

MySQL AND Condition

To find if all of the columns of those you list are null, the following PHP code can be used, shown below.



If all of these columns you listed have null values, the row will be returned.


Related Resources

How to Add a Column to a MySQL Table Using PHP

How to Add a Primary Key to a Column of a MySQL Table Using PHP

How to Make a Column of a MySQL Table Unique Using PHP

How to Rename a Column of a MySQL Table Using PHP

How to Delete a Column of a MySQL Table Using PHP

How to Set the Default Value of a Column of a MySQL Table Using PHP

How to Drop the Default Value of a Column Of a MySQL Table Using PHP

How to Modify the Defintion or Data Type of a Column of a MySQL Table Using PHP

How to Insert Data into a MySQL Table Using PHP

How to Delete a Row of a MySQL Table Using PHP

How to Delete All Rows of a MySQL Table Using PHP

How to Insert Data Into a Table of a MySQL Database From an HTML Form Using PHP

How to Update a Value in a MySQL Table Using PHP

How to Copy a Table Definition in MySQL Using PHP

HTML Comment Box is loading comments...