我想回复来自mySQL数据库的各种回复,而不重复它们。
表格如下:
+----+----------+---------------------+----------+
| id | user | timestamp | champion |
+----+----------+---------------------+----------+
| 1 | Watrasei | 2015-11-13 22:31:27 | yasuo |
+----+----------+---------------------+----------+
| 2 | Meta Nex | 2016-04-29 02:47:33 | yasuo |
+----+----------+---------------------+----------+
| 3 | Meta Nex | 2016-04-30 02:27:53 | yasuo |
+----+----------+---------------------+----------+
当我直接在phpMyAdmin中输入此代码时,响应是预期的:
SELECT DISTINCT user FROM champions3 WHERE champion = 'yasuo'
然而,当我使用这个PHP代码发出请求并回显结果时,只显示最后的结果:
<?php
$dbconnect = new MySQLi("localhost","root","pass","database");
if ($dbconnect ->connect_errno){
die("Connection failed: " . $dbconnect ->connect_error);
}
$creatorSQL = "
SELECT DISTINCT user
FROM champions3
WHERE champion = 'yasuo'
";
$creatorQuery = $dbconnect->query($creatorSQL);
// Convert all results into variables. If there is more than one result, put a comma in front of the name.
$i = 1;
while ($creator = mysqli_fetch_assoc($creatorQuery)) {
if($i = 1) {
${'author'.$i} = '<a>'.$creator["user"].'</a>';
}
else {
${'author'.$i} = ', <a>'.$creator["user"].'</a>';
}
$i++;
}
echo $author1, $author2, $author3, $author4, $author5, $author6, $author7;
mysqli_close($dbconnect);
?>
预期结果应为:Meta Nex,Watrasei
提前谢谢。
答案 0 :(得分:0)
<?php
$dbconnect = new MySQLi("localhost","root","pass","database");
if ($dbconnect ->connect_errno){
die("Connection failed: " . $dbconnect ->connect_error);
}
$creatorSQL = "
SELECT DISTINCT user
FROM champions3
WHERE champion = 'yasuo'
";
$creatorQuery = $dbconnect->query($creatorSQL);
// Convert all results into variables. If there is more than one result, put a comma in front of the name.
$i = 1;
while ($creator = mysqli_fetch_assoc($creatorQuery)) {
if($i == 1) {
${'author'.$i} = '<a>'.$creator["user"].'</a>';
}
else {
${'author'.$i} = ', <a>'.$creator["user"].'</a>';
}
$i++;
}
echo $author1, $author2, $author3, $author4, $author5, $author6, $author7;
mysqli_close($dblogin);
?>
答案 1 :(得分:0)
检查你的mysqli_close()
供参考:
<?php
$dbconnect = new mysqli("localhost","root","123456","testing");
if ($dbconnect ->connect_errno){
die("Connection failed: " . $dbconnect ->connect_error);
}
$creatorSQL = "
SELECT DISTINCT name
FROM user_detail
WHERE Age = 23
";
$creatorQuery = $dbconnect->query($creatorSQL);
// Convert all results into variables. If there is more than one result, put a comma in front of the name.
while ($creator = mysqli_fetch_assoc($creatorQuery)) {
$author[] = $creator['name'];
}
echo implode(', ', $author);
mysqli_close($dbconnect);
?>