I am having trouble on how to display/echo single name from multiple value of (three) of the same name. My table name is usages
.
id | account | amount | date
1 Purchase 10000 2015-04-01
2 Repair 200000 2015-04-02
3 Purchae 30000 2015-04-03
4 Purchase 10000 2015-04-04
5 Mafanikio 20000 2015-04-04
6 Simon 20000 2015-04-04
7 Spare 10000 2015-04-04
This is my PHP code:
global $database;
$i = 1;
$seL = "SELECT * FROM usages ";
$Q =$database->query($seL);
$num = $database->query_to_num_rows($seL);
if($num !=0) {
while ($fet =$database->fetch_array($Q)) {
$show =$fet['account'];
$sel2 ="SELECT * FROM usages WHERE account = '$show' GROUP BY account ";
$Que =$database->query($sel2);
$numR =$database->query_to_num_rows($sel2);
if($numR ==1) {
$arr = $database->fetch_array($Que);
echo "<br> ".$i++. " ". $a =$arr['account'];
The result here is:
1 Purchase
2 Repair
3 Purchase
4 Purchase
5 Mafanikio
6 Simon
7 Spare
My desired answer I need like this
1 Purchase
2 Repair
3 Mafanikio
4 Simon
5 Spare
I don't want the same name to repeat.
use DISTINCT
In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.
Try this as your query:
"SELECT DISTINCT * FROM usages WHERE account = '$show'";
"SELECT DISTINCT * FROM usages ";
use whichever your are trying to get single name from.
global $database;
$i = 1;
$seL = "SELECT DISTINCT account FROM usages ";
$Q =$database->query($seL);
$num = $database->query_to_num_rows($seL);
if($num !=0) {
while ($fet =$database->fetch_array($Q)) {
$show =$fet['account'];
$sel2 ="SELECT * FROM usages WHERE account = '$show'";
$Que =$database->query($sel2);
$numR =$database->query_to_num_rows($sel2);
if($numR ==1) {
$arr = $database->fetch_array($Que);
echo "<br> ".$i++. " ". $a =$arr['account'];