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

10
  • php.net/manual/en/function.array-unique.php Commented Dec 11, 2016 at 10:52
  • can you give me the solution? Commented Dec 11, 2016 at 10:52
  • 2
    you mean SELECT DISTINCT? Commented Dec 11, 2016 at 10:55
  • i've entered duplicate values in mysql, now i want that when i fetch them then i'll get only 1 value, rest of the duplicate values should be deleted Commented Dec 11, 2016 at 10:56
  • stackoverflow.com/questions/854128/… Commented Dec 11, 2016 at 10:57

3 Answers 3

1

Your query should be

SELECT * FROM areas GROUP BY id

I have tested it.

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

Comments

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]'},";
}

1 Comment

I forgot a $. Please be more specific and try to debug yourself before asking for more help
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

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.