0

here's the code and i want to echo only 1 city from mysql database!

<?php


include('db.php');

    $queryss = mysqli_query($conn, 'SELECT * FROM areas');

    while ($rowx = mysqli_fetch_array($queryss)) {


        echo "{pro:'$rowx[1]',city:'$rowx[2]', dist:'$rowx[3]', town:'$rowx[4]', area:'$rowx[5]',subarea:'$rowx[6]',ucname:'$rowx[7]'},";

    }

    ?>

and i'm, getting this input here! 3 time karachi in my html, but i want only 1 of this city. SELECT DISTINCT is working in mysql but how can i use it in PHP?

enter image description here

David Duponchel
  • 3,959
  • 3
  • 28
  • 36
Jawad Ajaz
  • 9
  • 1
  • 6

3 Answers3

1

Your query should be

SELECT * FROM areas GROUP BY id

I have tested it.

Rajesh
  • 30
  • 7
0

Use

SELECT DISTINCT column_name1,column_name2 FRAM areas

in your SQL where column_nameN stands for the columns you need for your output.

OR use something like this (untested):

$results = [];
while ($rowx = mysqli_fetch_array($queryss)) {
    $results[] = $rowx;
}
$results= array_unique($results);
foreach($results as $rowx) {
    echo "{pro:'$rowx[1]',city:'$rowx[2]', dist:'$rowx[3]', town:'$rowx[4]', area:'$rowx[5]',subarea:'$rowx[6]',ucname:'$rowx[7]'},";
}
Iarwa1n
  • 460
  • 3
  • 12
0

First Solution

You can insert distinct keyword into your SQL to accomplish what you need, like so

SELECT DISTINCT your_column_name FROM table_name

Second Soltion

You can execute your SQL statement and then use array_unique, to be like so

$selectStatement = mysqli_query($con, 'SELECT * FROM areas');

$selectedArrayValues = mysqli_fetch_array($selectStatement);

$selectedUniqueArrayValues = array_unique(selectedArrayValues);

// Then return that array to your HTML code

I recommend the first solution because it's more optimized