This question already has answers here:
PHP parse/syntax errors; and how to solve them
                                
                                    (17个答案)
                                
                        
                                3年前关闭。
            
                    
我是mysql的新手,下面是我尝试从数据库中获取下拉列表中的值但出错的代码。


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


码:

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>" . $row['ticket_no']. "</td>";
echo "<td align='center'> <select>
<?php
$host='localhost'; // Host name
$username='root'; // Mysql username
$password=''; // Mysql password
$db_name='testing'; // Database name

// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die('cannot connect');

mysqli_select_db($conn,$db_name);

$username='SELECT name from user where role="Support"';
$uresult=mysqli_query($username) or die(.mysqli_error());
while($rows=mysqli_fetch_array($uresult))
{
$name=$rows['name'];
echo '<option>$name</option>';
}
?></select> </td>";
echo "</tr>";
}

最佳答案

这样,它应该工作:

<?php
while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td align='center'>" . $row['ticket_no']. "</td>";
    echo "<td align='center'> <select>";
    $host='localhost'; // Host name
    $username='root'; // Mysql username
    $password=''; // Mysql password
    $db_name='testing'; // Database name

    // Connect to server and select databse.
    $conn=mysqli_connect($host,$username,$password) or die('cannot connect');

    mysqli_select_db($conn,$db_name);

    $username='SELECT name from user where role="Support"';
    $uresult=mysqli_query($username) or die(mysqli_error($conn));
    while($rows=mysqli_fetch_array($uresult))
    {
        $name=$rows['name'];
        echo "<option>$name</option>";
    }
    echo "</select></td>";
    echo "</tr>";
}
?>


但是最终最好一次查询所有用户,而不是在外部while循环的每个迭代中都选择它们。

09-19 05:49