将 MySql 数据提取到<选择>中


Fetch MySql data into <select>

试图将 mysql 中的值获取到<select>标签中。
我使用 while 循环并从 2 个不同的表中获取数据。
我已经尝试了给定的代码,但无法获取数据。
请帮忙!

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name  
// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
mysqli_select_db($conn,$db_name);
$result = mysqli_query($conn,"SELECT * from dummy");
$uresult=mysqli_query($conn,"SELECT name from user where role='Support'"); 
$uname=array();
while ($rows=mysqli_fetch_array($uresult))
{
$uname[]=$rows['name'];
}
echo "<table border='1' width='100%' >
<tr><th colspan='7' ><h2 align='center'>Details</h2></th></tr>
<tr bgcolor='grey'>
<th width='10%' class='text-center'>Emp No.</th>
<th width='15%' class='text-center'>Name</th>
<th width='30%' class='text-center'>Task</th>
<th width='20%' class='text-center'>Assigned To</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>" . $row['emp_no']. "</td>";
echo "<td align='center'>" . $row['emp_name']. "</td>";
echo "<td align='center'>" . $row['task'] . "</td>";
echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>

错误:解析错误:语法错误、意外的"(T_ENCAPSED_AND_WHITESPACE)、期望标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING)

错误与以下行字符串中有 PHP 代码

echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>";

无需再次获取它。您已经在此处完成此操作:

$uname = array();
while ($rows = mysqli_fetch_array($uresult)) {
    $uname[] = $rows['name'];
}

因此,在另一个 while 块中,只需使用已经收集的名称:

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
        echo "<td align='center'>" . $row['emp_no']. "</td>";
        echo "<td align='center'>" . $row['emp_name']. "</td>";
        echo "<td align='center'>" . $row['task'] . "</td>";
        echo "
        <td align='center'>
            <select name='select1'>
        ";
        foreach($uname as $names) {
            echo '<option>' . $names . '</option>';
        }
        echo "
            </select>
        </td>";
    echo "</tr>";
}

或者只是用$uname创建一个 HTML <options>字符串,然后将其echowhile中:

$uname = '';
while ($rows = mysqli_fetch_array($uresult)) {
    $uname .= '<option>' . $rows['name'] . '</option>';
}

然后在while块上:

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
        echo "<td align='center'>" . $row['emp_no']. "</td>";
        echo "<td align='center'>" . $row['emp_name']. "</td>";
        echo "<td align='center'>" . $row['task'] . "</td>";
        echo "
        <td align='center'>
            <select name='select1'>
        ";
        echo $uname; // collection of HTML string options
        echo "
            </select>
        </td>";
    echo "</tr>";
}

像这样尝试,在 html 中embedding php

while($row = mysqli_fetch_array($result))
{
    ?>
    <tr>
    <td align='center'><?php echo $row['emp_no'] ?></td>
    <td align='center'><?php echo $row['emp_name'] ?></td>
    <td align='center'><?php echo $row['task'] ?></td>
    <td align='center'> <select name='select1'> 
    <?php while ($rows=mysqli_fetch_array($uresult)) { ?>
    <option><?php echo $rows['name'];?> </option><?php } ?> 
    </select> </td>
    </tr>";
    <?php 
}