使用php中的值将新选项添加到select中

使用php中的值将新选项添加到select中,php,html,mysql,html-select,Php,Html,Mysql,Html Select,我想添加一个动态选择列表,其中包含来自php的数据。我该怎么做?我还希望使用其他php文件中的变量作为参数,例如$mentor==$rows。感激 <th><select name="mentor" style="width:95%"> <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = '';

我想添加一个动态选择列表,其中包含来自php的数据。我该怎么做?我还希望使用其他php文件中的变量作为参数,例如$mentor==$rows。感激

<th><select name="mentor" style="width:95%">
        <?php
            $dbhost = 'localhost';
            $dbuser = 'root';
            $dbpass = '';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);
            mysql_select_db("testproject", $conn);

            if(! $conn )
            {
                die('Could not connect: ' . mysql_error());
            }

            $sql = "SELECT mtrname FROM mentors";
            $sql2 = "SHOW COLUMNS from mentors";
            $result= mysql_query( $sql, $conn );
            if(! $result)
            {
                die('Could not get data: ' . mysql_error());
            }
            $fetchrow = mysql_fetch_row($sql2);
            $num = count($fetchrow);
            while($rows = mysql_fetch_array($result))
            {
                for($i=0;$i<$num;$i++)
                {
                     $rows[$i];
                }
            }   
            mysql_close($conn);
        ?>
        <option value="<?php echo $mentor?>" <?php if($mentor==$rows[i]) echo 'selected'?>><?php echo $mentor?></option>
        </select></th>

>
编辑: 因此,当用户第一次包含数据时,我尝试将select选项放入。我试过的是
您应该从mysql迁移,因为它没有被取消,应该迁移到PDO或mysqli。如果返回的结果是正确的,那么您就差不多做到了。只需将
选项
标记移动到
for()循环中即可

while($rows = mysql_fetch_array($result))
{
    for($i=0;$i<$num;$i++)
    {
         echo "<option value='$mentor'". ($mentor==$rows[i] ? 'selected' : '') .">$mentor</option>";
    }
}
while($rows=mysql\u fetch\u array($result))
{

for($i=0;$i将选项标记移动到
for()的内部
loop,current它在
循环之外,而
循环没有引用
$rows
mysql并不重要,因为这只是一个大学项目。这是唯一不起作用的东西。仍然不起作用。你认为这是我的数据库问题吗?我的查询/php正确吗?