How to Display a MySQL Tables Without Duplicate Entries Using PHP


In this article, we show how to display a MySQL table without duplicate entries in any of the columns using PHP.

So, if a column in a table has duplicate entries, we only show it once in the table.

The general format of the MySQL query to select from the table where a value is distinct is shown below. shown below.

So the following code above selects only distinct values from the column, named column-name, from the table, named table_name.

Since the value in the column has to be distinct to be selected, once this query selects a value, it will not select another row in the column with the same exact value.

Below we show how to tie this in with PHP.

We'll now show how to tie this in with PHP to delete a row from a table.

So, first, we must connect to the database.

After we do this, we create a variable named $result. This $result variable queries the table in the column named column_name for distinct values. This is taken from the table named table_name. So once the query takes in a value, it will not take it in again unless it is different (or distinct).

So this is how we're able to avoid duplicate values in a table.

We can later use PHP and HTML code to output what this the rows chosen in table form.


An example of a table with duplicate values is shown below.

MySQL emails table

You can see is duplicated several times and is duplicated as well.

Using the PHP code above and inserting the table and column name into the code, we get the following result below, with no duplicate entries.

Actual PHP Output