1

My Database

Tables_sup |
+------------------+
| supid            |                
  scompany        | 
| scategory        | 
| smarket           |
|    

MY PHP Code

<?php
require_once 'DBConnect.php';



    $con = mysqli_connect("localhost","gb","123");
        if (!$con) {
        die('Could not connect: ' . mysqli_error());
        }

    mysqli_select_db($con,"test_database");



    $sql=mysqli_query($con,'SELECT DISTINCT sup.supid,sup.smarket FROM sup order by supid desc');
    $i=0;
    $dyn_table='<table border="1" cellpadding="10">';
    while($row=mysqli_fetch_array($sql)){

     $supid = $row["supid"];
     $smarket = $row["smarket"];

     if ($i%5==0){

        $dyn_table.='<tr><td>'.$smarket. '<td>';

      }else{

        $dyn_table.='<td>'.$smarket. '<td>';

     }
     $i++;
}

$dyn_table.= '<tr><table>' ;   
?>

MY HTML

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <?php echo $dyn_table;?>

</body>
</html>

My Question The result is OK, but with a duplicate.How to get the unique result?

6
  • 1
    You should check array_unique Commented Sep 21, 2017 at 8:57
  • 5
    Select distinct return only not duplicated so ... update you question with a datasample ., your actual result and the expected result Commented Sep 21, 2017 at 8:58
  • 1
    What is you expected result ?? Commented Sep 21, 2017 at 9:08
  • 1
    The result is coming with the duplicate.I am trying to add image of result, but I unable to attach Commented Sep 21, 2017 at 9:10
  • 1
    I want only unique result Commented Sep 21, 2017 at 9:11

2 Answers 2

1

Assuming that the id column of your sup table is the primary key and therefore unique anyway, your distinct query makes no sense - every found result has a different id anyway, therefore it can return multiple rows with the same smarket.

Query for distinct smarket (without supid) and you will get your desired resultset

SELECT DISTINCT smarket FROM sup

I see some mistakes in your tableconstruction as well (closing tr tags for example)

// every fifth row is gonna be different?
if ($i%5==0){
   $dyn_table.='<tr class="fifthrow"><td>'.$smarket. '</td></tr>';
}else{
   $dyn_table.='<tr><td>'.$smarket. '</td></tr>';
}

and in the end, dont open another tr and table ;) $dyn_table.= '</table>' ;

edit: forgot to add some closing tags for table

Sign up to request clarification or add additional context in comments.

Comments

1

in your circumstance, you can try group by function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.