tags:

views:

50

answers:

3

Hello,

I am confused here.

$sql_n = mysql_query("SELECT * FROM table1 WHERE n='{$row['n']}'");
$row_n = mysql_fetch_array($sql_n);

$sql= mysql_query("SELECT DISTINCT p FROM table1");
while($row = mysql_fetch_array($sql)) { 

    if($row['p'] == $row_n['p']) {
        $selected = " selected"; 
    }

    $np .= "<option value='{$row['p']}'$selected>{$row['p']}</option>"; 
}

When i use query SELECT DISTINCT p, $selected isn't working, however if i use SELECT p. Its working, any idea why?

A: 

Without doing PHP, but knowing how engines will sometimes make up column names if you don't explicitly imply the "as" column name, it may be doing something with the distinct such as

select DISTINCT p as DISTINCT_P

and thus your column name "p" in the result query does't exist.

You may want to try

Select DISTINCT P P

so the implied result column name is "P" and qualify the rest of your routine.

DRapp
i already tried this before, its still same :(
cicakman
A: 

My first reaction is that the solution to your problem would be to add DISTINCT in your first query as well.

Java Dude
A: 

Thinking it was the query, now I don't think that's it... but instead, your $selected variable. Its not declared anywhere until the actual

if($row['p'] == $row_n['p']) { 
        $selected = " selected";  
    } 

you probably need to "else" this and set

$selected = " ";

so it won't be a variable doesn't exist which is failing it when you are trying to concatinate it in the string building below when building out the OPTIONS list.

DRapp