PHP下拉菜单
问题描述:
<br><h2>Select a Tag</h2></br>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("portal", $con);
$query = "SELECT tag_name FROM tags";
$result = mysql_query($query);
?>
<select name="tag_name" id="abc">
<option size=30 selected>Select</option>
<?php
while($array = mysql_fetch_assoc($result)){
?>
<option value ="<?php echo $array['tag_name'];?>"><?php echo $array['tag_name'];?>
</option>
<?php
}
?>
</select> <br><br>
这是获取页面下拉菜单的代码片段。 我有一个数据库名为门户和表命名标签tag_name作为属性。 帮助我找到程序中的错误。 我不是在下拉菜单中PHP下拉菜单
答
第一件事: 你能告诉我如果计数大于零? 尝试使用“SELECT tag_name` FROM tags”(带有怪异的引号)而不是“SELECT tag_name FROM tags”来制作select语句。
答
<br><h2>Select a Tag</h2></br>
<?php
// Connect to database
// NEVER use the root user in a production environment!
$con = mysql_connect("localhost","root","");
if (!$con) {
// NEVER show the result of mysql_error() in a production environment!
die('Could not connect: ' . mysql_error());
}
mysql_select_db("portal", $con);
// Run the query (and check the result)
$query = "SELECT tag_name FROM tags";
if (!$result = mysql_query($query)) {
die("MySQL error at query: ".mysql_error());
}
if (mysql_num_rows($result)) {
// Draw the select if the query returned more than 0 rows, or display an error
?>
<select name="tag_name" id="abc">
<option size=30 selected>Select</option>
<?php while ($array = mysql_fetch_assoc($result)) { ?>
<option value="<?php echo $array['tag_name'];?>"><?php echo $array['tag_name'];?></option>
<?php } ?>
</select><br><br>
<?php } else { ?>
Query returned 0 results!
<?php } ?>
+0
查询返回0结果,这是任何原因。 – rShetty
+0
因为服务器'localhost'的数据库'portal'中的表'标签'没有记录。 – DaveRandom
如果没有别的,你有两个'
你确定你的查询是确定的,你有任何结果吗? – Dusan
杜尚:如何检查? – rShetty