我有一个数据库,我正在尝试使用带有foreach的PHP获取列表,但列表返回如下:
RiquelmeMacielLewBrMarcuus
但它是这样的:
RiquelmeMaciel
LewBr
Marcuus
我的代码:
<?php
include_once("conexao.php");
$select = "SELECT * FROM usuarios ORDER BY id";
$query = mysqli_query($conn, $select);
$rows = mysqli_fetch_assoc($query);
?>
<?php foreach($rows as $row){
echo '<td class="mdl-data-table__cell--non-numeric name">'.$row['nome'].'</td>';
} ?>
答案 0 :(得分:1)
您需要插入表行来创建行:
<?php
//Incluindo a conexão com banco de dados
include_once("conexao.php");
$result_usuario = "SELECT * FROM usuarios ORDER BY id";
$resultado_usuario = mysqli_query($conn, $result_usuario);
$resultado = mysqli_fetch_assoc($resultado_usuario);
foreach($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric"></td>';
echo '<td class="mdl-data-table__cell--non-numeric"></td>';
echo '</tr>';
}
现在您只需要为'EMAIL'和'SENHA'添加变量。
答案 1 :(得分:0)
foreach循环中的问题 - 对于输出标记的eache元素,但是是表格单元格标记。
如果要在3列表中填写第一列,则必须执行以下操作:
<?php foreach($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td>second column</td>';
echo '<td>third column</td>';
echo '</tr>';
} ?>
答案 2 :(得分:0)
您以错误的方式显示数据,<td>
应位于<tr>
内<table>
:
<table>
<tr>
<th>Nome</th>
<th>Email</th>
<th>Senha</th>
</tr>
<?php
foreach ($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric email">'.$teste['email'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric senha">'.$teste['senha'].'</td>';
echo '</tr>';
}
?>
</table>
然后会根据需要显示数据。看看HTML - table。
如果您想要从 SQL查询更改数据的顺序,请使用ORDER BY ... ASC|DESC。