How to Display a MySQL Tables Without Duplicate Entries Using PHP



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.

Example

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

MySQL emails table

You can see abc@gmail.com is duplicated several times and go@gmail.com 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


abc@gmail.com
go@gmail.com
peter@gmail.com
michelle@gmail.com
josh@gmail.com



Related Resources

How to Create a MySQL Table Using PHP

How to Select a Table from a MySQL Database

How to Check if a MySQL Table Exists Using PHP

How to Show All Tables of a MySQL Database Using PHP

How to Delete a MySQL Table 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...