我有一个代码,当一个人登录时,它会显示与他们的帐户自动相关的某些信息。我正在尝试使它在下拉列表中列出信息,并将每个结果作为新选项。现在,它正在发布结果,但结果都在同一行。
此图显示了查询当前如何输出结果。我需要将每个名称都放在单独的行中,而不是使用相同的选项。
<?php
$results = mysqli_query($db, "SELECT u.Name AS Name, u.activation as activation, u.lastlogin AS lastlogin, u.dwForbidTime AS dwForbidTime, u.RegDate AS RegDate, u.email AS email, group_concat(distinct g.szName SEPARATOR '<br />') AS szName, g.byShape as byShape, g.Level as Level
FROM user AS u
LEFT JOIN gamedata AS g on u.name = g.szAccountName
WHERE Name='$user_check'
ORDER BY g.szAccountName DESC
LIMIT 3") or die("Error: " . mysqli_error($db));
echo "<select name='character'>";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<option value="' . $row['szName'] . '">' . $row['szName'] . '</option>';
}
echo "</select>";
?>
如果我不尝试使用下拉列表,但我需要为表单提供选择选项,则我的查询输出会很好。
答案 0 :(得分:1)
请勿使用GROUP_CONCAT()
。就是将您所有的结果合并为一行,每个名称之间使用<br>
。您的while
循环希望每个名称都在单独的结果行中。
$results = mysqli_query($db, "
SELECT distinct g.szName
FROM user AS u
LEFT JOIN gamedata AS g on u.name = g.szAccountName
WHERE Name='$user_check'
ORDER BY g.szAccountName DESC
LIMIT 3") or die("Error: " . mysqli_error($db));
如果您仅在下拉菜单中显示szName
,则也不需要选择所有其他列。
答案 1 :(得分:-1)
I used a foreach for testing this and it worked perfectly.
$row = array('test', 'test 1', 'test 2');
echo "<select name='character'>";
foreach ($row as $result) {
echo "<option value='$result'>$result</option>";
}
echo "</select>";