Php MYSQLi多记录输出重复
问题:Php MYSQLi多记录输出重复,php,html,mysql,mysqli,Php,Html,Mysql,Mysqli,问题: global $connection; //Query database & retrieve results. $search = $connection->query('SELECT * FROM users WHERE country= "'.$country.'"'); echo '<table><tr><th>Username</th><th>
global $connection;
//Query database & retrieve results.
$search = $connection->query('SELECT * FROM users WHERE country= "'.$country.'"');
echo '<table><tr><th>Username</th><th>Email</th><th>Country</th></tr>';
while ($result = $search->fetch_assoc())
{
$total[] = $result;
foreach ($total as $rows)
{
echo '<tr>';
echo '<td>'.stripslashes($rows['username']).'</td>';
echo '<td>'.stripslashes($rows['email']).'</td>';
echo '<td>'.stripslashes($rows['country']).'</td>';
echo '</tr>';
}
}
echo '</table>';
我一直在查看我的代码,无法找到数组复制数据的原因。数据库中的单个条目有一个正确的输出,但是如果有多个条目,它将复制之前的条目以及下一个条目
我的代码:
global $connection;
//Query database & retrieve results.
$search = $connection->query('SELECT * FROM users WHERE country= "'.$country.'"');
echo '<table><tr><th>Username</th><th>Email</th><th>Country</th></tr>';
while ($result = $search->fetch_assoc())
{
$total[] = $result;
foreach ($total as $rows)
{
echo '<tr>';
echo '<td>'.stripslashes($rows['username']).'</td>';
echo '<td>'.stripslashes($rows['email']).'</td>';
echo '<td>'.stripslashes($rows['country']).'</td>';
echo '</tr>';
}
}
echo '</table>';
你可以看到第一行是重复的,然后新的条目就在那里,它将继续从这一点上更多地继续下去
其他信息:
global $connection;
//Query database & retrieve results.
$search = $connection->query('SELECT * FROM users WHERE country= "'.$country.'"');
echo '<table><tr><th>Username</th><th>Email</th><th>Country</th></tr>';
while ($result = $search->fetch_assoc())
{
$total[] = $result;
foreach ($total as $rows)
{
echo '<tr>';
echo '<td>'.stripslashes($rows['username']).'</td>';
echo '<td>'.stripslashes($rows['email']).'</td>';
echo '<td>'.stripslashes($rows['country']).'</td>';
echo '</tr>';
}
}
echo '</table>';
- 这被放置在函数内部,并在提交$\u POST['country']时执行-$country只是$\u POST['country'],带有一个真正的转义字符串
- $connection只是脚本中引用的一个新的mysqli(localhost、user、pass、database)——它是全球化的,因为它被包装在一个函数中
如有任何帮助,我们将不胜感激。如我在评论中所述,将foreach置于while循环之外,如下所示:
echo '<table><tr><th>Username</th><th>Email</th><th>Country</th></tr>';
while ($result = $search->fetch_assoc()) {
$total[] = $result;
}
foreach ($total as $rows) {
echo '<tr>';
echo '<td>'.stripslashes($rows['username']).'</td>';
echo '<td>'.stripslashes($rows['email']).'</td>';
echo '<td>'.stripslashes($rows['country']).'</td>';
echo '</tr>';
}
echo '</table>';
非常简单。。。将项添加到数组中。。循环并继续向该数组添加项目,并继续循环..你能告诉我们你是如何调用该函数的吗?@Naruto所以你说$result=array('username'=>stripslashes('rows['username']),'email'=>stripslashes('rows['email']),'country=>stripslashes('rows['country']);然后我让它在foreach函数中重复,直到完成为止?@Nick检查我的答案;)@Mohamadatat当整数变为活动时,在if语句中调用引用,这意味着将执行从0到1的操作。此处显示:如果($active==1){searchcountry($country);}如果数据在数据库行的获取之外,foreach将如何获取数据?信息直接从数据库中获取,并通过查询按国家进行排序。你能插入一段你确切的意思吗?你知道[]方括号代表什么吗?你的代码与我的完全相同,我问你能否提供一段代码来更清楚地表达你的观点。[]表示数组;这是一个简短的形式。我的代码不是,也从来不是你的代码的副本。。如果你知道括号的意思,你可以自己轻松地解决这个问题。在保存函数以存储在数组中以供其他地方使用,而不仅仅是在表中显示所有结果方面,它不会以这种方式工作。这将两个相同的数组存储在另一个数组中:数组(数组([0]=>exampleUser1,[1]=>exampleEmail1@example.com,[3]=>美国)数组([0]=>exampleUser2[1]=>exampleEmail2@example.com,[3]=>美国)因此,当我想通过$row[0]使用第一个帐户的结果时,它会回显“exampleUser1exampleUser2”-两者都连接在一起,有没有办法分开?($total[]创建了一个环绕它的数组)我不太理解这个问题。请提供一些代码我通过一个简单的布局和这里要求的所有代码使它更具信息性:$total=array();而(/*循环条件*/){//…$total[]=$row;}$usernames=array_column($total,'Username');打印(用户名);在循环中使用不带分隔符的echo
时,您将得到texttext
。尝试echo“{$username[0]}\n”如果仍要在循环中使用echo
,请使用code>或其他分隔符
<?php
//... Your code
while ($rows = $search->fetch_assoc()) {
$rows = array_map('stripslashes', $rows);
echo '<tr>';
echo '<td>'.$rows['username'].'</td>';
echo '<td>'.$rows['email'].'</td>';
echo '<td>'.$rows['country'].'</td>';
echo '</tr>';
}
//... Your code