php mysql select dropdown selected and distinct query
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开发者_如何学JAVA SELECT p
. Its working, any idea why?
My first reaction is that the solution to your problem would be to add DISTINCT in your first query as well.
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.
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.
精彩评论