-1
 //select parent_id - display unique value
<?php
//database connection
$con = mysql_connect("xxxx","xxxx","xxxx");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("btreedata", $con);
//select distinct query
$query  = mysql_query("SELECT DISTINCT parent_id FROM btree");
$result = mysql_query($sql); 
//dropdown list
echo "<select name='parent_id'>"; 
while ($row = mysql_fetch_array($result)) { 
    echo "<option value='".$row[0]."'>".$row[0]."</option>"; 
} 
echo "</select>"; 
?>

output show like this:

1000
1000
1004
1004
1003
1001
1005
1001

how can I display only

1000
1001
1003
1004
1005
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • This would not appear to be your actual code - as setting $sql isnt shown. Please show your full code - also if you wanted your IDs sorted, you should add a sort condition. – BugFinder Oct 15 '12 at 07:34
  • 1
    I suspect your query is not that simple, otherwise it would return only the distinct values you expect. Although, you are using the query `$sql`...maybe you meant to use `$query`? – lc. Oct 15 '12 at 07:34
  • which type is "parent_id"? is this column defined as int or as (v)char? – donald123 Oct 15 '12 at 07:35
  • 4
    If you run this directly on the database: SELECT DISTINCT parent_id FROM btree, what output do you get? – Rob Oct 15 '12 at 07:35
  • I HAVE TO DISPLAY DROPDOWN LIST LIKE: – user1740287 Oct 15 '12 at 07:36
  • you use $result = mysql_query($sql); ,but $sql is missing in code,check that variable ,that it contains correct sql statement or not. – Saurabh Oct 15 '12 at 07:40
  • You are running two queries: `$query = mysql_query("SELECT DISTINCT parent_id FROM btree");` and `$result = mysql_query($sql);`. And you even did not share `$sql` with your question. – hakre Oct 15 '12 at 07:50
  • Also please see http://stackoverflow.com/questions/7250566/mysql-select-distinct - Your code should just work by the way, you're just a bit chaotic about it I'd assume. – hakre Oct 15 '12 at 07:52

1 Answers1

0
SELECT  parent_id FROM btree group by parent_id order by parent_id ASC
Man Programmer
  • 5,300
  • 2
  • 21
  • 21