我正在尝试检索我的成员表中列出的所有用户名,将它们列在页面中并将它们链接到用户配置文件。这是我检索成员表的用户名的方法:
$result = mysqli_query($mysqli,"SELECT username FROM members");
$row = mysqli_fetch_array($result);
$username = $row['username'];
为了链接它们,我将使用以下内容:
<?php
foreach ($result as $row) {
?>
<a href="#"><?php echo $username ?></a>
<?php
}
?>
问题是它只显示1个用户名,5次。所以我想:
USERNAME1 USERNAME1 USERNAME1 USERNAME1 USERNAME1
而不是
USERNAME1 USERNAME2 USERNAME3 username4 username5
我做错了什么? (对不起,最近开始学习PHP / MYSQL,所以我对它很新。
答案 0 :(得分:1)
因为你只获得了第一个结果。尝试:
$users = array();
$result = mysqli_query($mysqli,"SELECT username FROM members");
while($row = mysqli_fetch_assoc($result)){
$users[] = $row['username'];
}
<?php
foreach ($users as $user): ?>
<a href="#"><?php echo $user; ?></a>
<?php endforeach; ?>
答案 1 :(得分:0)
试试这个:
$result = mysqli_query($mysqli,"SELECT username FROM members");
$row = mysqli_fetch_array($result);
<?php
foreach ($result as $row) {
$username = $row['username'];
?>
<a href="#"><?php echo $username ?></a>
<?php
}
?>
您需要获取行中每个项目的用户名。
答案 2 :(得分:0)
$result = mysqli_query($mysqli,"SELECT username FROM members");
$count=0;
while($row = mysqli_fetch_array($result)){
array[$count]['Rkey'] = $count;
$array[$count]['username']= $row['username'];
$count++;
}
mysqli_free_result($result);
}
打印值
$ res_count = count($ array);
for ($x=0;$x<$res_count;$x++)
{
echo $array[$x]['username'];
}