php 在选择框中显示 Mysql 表字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14372860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Mysql table field values in Select box
提问by Prusothaman Varatharaj
I want to display the Mysql table Filed values in selectbox. I tried the following code to display. But it normally display the specified field values in echo function and not in select box. I don't know where I mistake.
我想在选择框中显示 Mysql 表 Filed 值。我尝试了以下代码来显示。但它通常在 echo 函数中显示指定的字段值,而不是在选择框中。我不知道我错在哪里。
$con = mysql_connect("localhost","root","root");
$db = mysql_select_db("Time_sheet",$con);
$get=mysql_query("SELECT Emp_id FROM Employee");
while($row = mysql_fetch_assoc($get))
{
echo ($row['Emp_id']."<br/>");
}
<html>
<body>
<form>
<select>
<option value = "<?php echo($row['Emp_id'])?>" ><?php echo($row['Emp_id']) ?></option>
</select>
</form>
</body>
</html>
Also the field values must be display in ascending order. How to achieve..
此外,字段值必须按升序显示。如何实现..
回答by asprin
<?php
$con = mysql_connect("localhost","root","root");
$db = mysql_select_db("Time_sheet",$con);
$get=mysql_query("SELECT Emp_id FROM Employee ORDER BY Emp_id ASC");
$option = '';
while($row = mysql_fetch_assoc($get))
{
$option .= '<option value = "'.$row['Emp_id'].'">'.$row['Emp_id'].'</option>';
}
?>
<html>
<body>
<form>
<select>
<?php echo $option; ?>
</select>
</form>
</body>
</html>
PS : On a sidenote, please stop using mysql_*functions. Take a look at thisthread for the reasons.
PS:附带说明,请停止使用mysql_*功能。看看这个线程的原因。
回答by Muhammad Raheel
You can easily do it like this
你可以很容易地这样做
$con = mysql_connect("localhost","root","root");
$db = mysql_select_db("Time_sheet",$con);
$get=mysql_query("SELECT Emp_id FROM Employee");
<html>
<body>
<form>
<select>
<option value="0">Please Select</option>
<?php
while($row = mysql_fetch_assoc($get))
{
?>
<option value = "<?php echo($row['Emp_id'])?>" >
<?php echo($row['Emp_id']) ?>
</option>
<?php
}
?>
</select>
</form>
</body>
</html>
回答by Suhel Meman
You have to use while loop to display option in select box. try this ...
您必须使用 while 循环在选择框中显示选项。尝试这个 ...
$con = mysql_connect("localhost","root","root");
$db = mysql_select_db("Time_sheet",$con);
$get=mysql_query("SELECT Emp_id FROM Employee order by Emp_id");
<html>
<body>
<form>
<select>
<?php
while($row = mysql_fetch_assoc($get))
{
?>
<option value="<?php echo $row['Emp_id']; ?>"><?php echo $row['Emp_id']; ?></option>
<?php
}
?>
</select>
</form>
</body>
</html>
回答by Deepak Srinivasan
<?php
$con = mysql_connect("localhost","root","root");
$db = mysql_select_db("Time_sheet",$con);
$get=mysql_query("SELECT Emp_id FROM Employee");
?>
<html>
<body>
<form>
<select>
<?php
while($row = mysql_fetch_assoc($get)){?>
<option value = "<?php echo($row['Emp_id'])?>" ></option>
<?php } ?>
</select>
</form>
</body>
回答by rashedcs
<?php
$con = mysql_connect("localhost","root","root");
$db = mysql_select_db("Time_sheet",$con);
$res=mysql_query("SELECT Emp_id FROM Employee");
?>
<html>
<body>
<form>
<select>
<?php
while ($row = $res->fetch_assoc())
{
echo '<option value=" '.$row['id'].' "> '.$row['name'].' </option>';
}
?>
</select>
<form>
</body>
</html>
回答by mickmackusa
There a few tips to offer that will condense the code block for this task -- too many to just comment under the accepted answer.
有一些提示可以压缩此任务的代码块 - 太多而无法仅在已接受的答案下进行评论。
Tested Code:
测试代码:
if (!$con = new mysqli("localhost", "root", "root", "Time_sheet")) {
echo "Database Connection Error: " , $con->connect_error; // don't show actual error in "production" stage
} elseif (!$result = $con->query("SELECT Emp_id FROM Employee ORDER BY Emp_id")) {
echo "Syntax Error: " , $con->error; // don't show actual error in "production" stage
} else {
echo "<select>";
foreach ($result as $row) {
echo "<option>{$row['Emp_id']}</option>";
}
echo "</select>";
}
- The
ifline is both declaring$conand checking it for a falsey return value in one step. - Never provide the raw connection or query errors to your users. It is okay during development, but you don't want strangers to have access to critical/informative errors that your server will provide. This is a basic best practice for security.
- The
elseifline is both declaring$resultand checking it for a falsey return value in one step. - The
elseblock will process the zero or more rows of data that were generated as a result of the successful SELECT query. - The mysqli result is "traversable" which means you can iterate it using
foreach()and enjoy direct access to the row data via associative keys. PHP Manual ReferenceAnother StackOverflow Post - When writing an array element into a literal string via interpolation, it is good practice (though not always necessary) to wrap the element in curly braces. This will often trigger helpful highlighting in IDEs and ensure that the characters that follow the array are not accidentally coupled to the variable name itself.
- You ONLY need to write a
valueattribute inside of the<option>tag IF the value is different from text between the opening and closing tags (<option>the text in here</option>). Form submissions and javascript implementations will all work the same if you omit the redundantvalueattribute. If you DO wish to submit a different value in the select field rather than the text, here is what the syntax can look like:
echo "<option value=\"{$row['Emp_id']}\">{$row['Emp_name']}</option>";If you want to write a
selectedattribute on one of the options based on a pre-existing variable (e.g.$selected_id), it can look like this:echo "<option" , ($row['Emp_id'] == $selected_id ? " selected" : "") , ">{$row['Emp_name']}</option>";If you wanted to combine the two previous processes, it can look like this:
echo "<option value=\"{$row['Emp_id']}\"" , ($row['Emp_id'] == $selected_id ? " selected" : "") , ">{$row['Emp_name']}</option>";
- 该
if行在$con一个步骤中既声明又检查它是否有错误的返回值。 - 永远不要向您的用户提供原始连接或查询错误。在开发过程中没问题,但您不希望陌生人访问您的服务器将提供的关键/信息性错误。这是安全的基本最佳实践。
- 该
elseif行在$result一个步骤中既声明又检查它是否有错误的返回值。 - 该
else块将处理作为成功 SELECT 查询的结果生成的零行或多行数据。 - mysqli 结果是“可遍历的”,这意味着您可以使用它进行迭代
foreach()并通过关联键直接访问行数据。 PHP 手册参考另一个 StackOverflow 帖子 - 通过插值将数组元素写入文字字符串时,将元素括在花括号中是一种很好的做法(尽管并非总是必要)。这通常会在 IDE 中触发有用的突出显示,并确保数组后面的字符不会意外地耦合到变量名称本身。
- 如果值与开始和结束标签之间的文本不同,您只需
value在<option>标签内写入一个属性(<option>the text in here</option>)。如果省略冗余value属性,表单提交和 javascript 实现都将工作相同。 如果您确实希望在选择字段而不是文本中提交不同的值,则语法如下所示:
echo "<option value=\"{$row['Emp_id']}\">{$row['Emp_name']}</option>";如果您想
selected根据预先存在的变量(例如$selected_id)在选项之一上编写属性,它可能如下所示:echo "<option" , ($row['Emp_id'] == $selected_id ? " selected" : "") , ">{$row['Emp_name']}</option>";如果你想结合前面的两个过程,它可以是这样的:
echo "<option value=\"{$row['Emp_id']}\"" , ($row['Emp_id'] == $selected_id ? " selected" : "") , ">{$row['Emp_name']}</option>";

