从mysql数据库php中输出三列
我是一个php初学者,我想从我的数据库中输出3个字段到我的网站,其中包括一个bloc图像、文本和名称。使用foreach,我只能从三个中得到两个从mysql数据库php中输出三列,php,arrays,database,mysqli,foreach,Php,Arrays,Database,Mysqli,Foreach,我是一个php初学者,我想从我的数据库中输出3个字段到我的网站,其中包括一个bloc图像、文本和名称。使用foreach,我只能从三个中得到两个 <table border="2px"> <tr> <th> Brand</th> <th> Images</th> <th> Details</th> </tr> <
<table border="2px">
<tr>
<th> Brand</th>
<th> Images</th>
<th> Details</th>
</tr>
<?php
$tex = [];
$query = mysqli_query($conn, "SELECT * FROM upload");
while ($row = mysqli_fetch_assoc($query)) {
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
}
foreach (array_combine($det, $images) as $text => $image) {
echo "<tr><td>";
echo $name;
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
echo "<td>";
echo $text;
echo "</td>";
echo "</td></tr>";
}
?>
</table>
有人能给我解释一下这些吗,因为我知道它在做什么,但我想成为100岁。这会有用:
while ($row = mysqli_fetch_assoc($query)) {
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
}
foreach ($images as $idx => $image) {
echo "<tr><td>";
echo $br[$idx];
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
echo "<td>";
echo $det[$idx];
echo "</td>";
echo "</td></tr>";
}
while($row=mysqli\u fetch\u assoc($query)){
$images[]=$row['image'];
$br[]=$row['name'];
$det[]=$row['text'];
}
foreach($idx=>$image的图像){
回声“;
echo$br[$idx];
回声“;
回声“;
回声';
回声“;
echo$det[$idx];
回声“;
回声“;
}
您只组合了
文本
和图像
。这样,您就不知道哪个名称
是对应的名称。我的循环通过使用索引($idx
)来解决这个问题。所有数组都具有相同的大小,因此这是可行的。像这样使用单独的foreach
循环是没有意义的。您只需更改,而循环如下:
while($row = mysqli_fetch_assoc($query)){
echo"<tr><td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'. base64_encode($row['image']) .'"/>';
echo"<td>";
echo $row['text'];
echo "</td>";
echo"</td></tr>";
}
while($row=mysqli\u fetch\u assoc($query)){
回声“;
echo$row['name'];
回声“;
回声“;
回声';
回声“;
echo$row['text'];
回声“;
回声“;
}
while($row = mysqli_fetch_assoc($query)){
echo"<tr><td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'. base64_encode($row['image']) .'"/>';
echo"<td>";
echo $row['text'];
echo "</td>";
echo"</td></tr>";
}