Php 选择1行,并将其显示在包含2列的表中
如何在数据库中创建一个Php 选择1行,并将其显示在包含2列的表中,php,mysql,Php,Mysql,如何在数据库中创建一个选择,并在一个包含2列的表中显示该行。 这是我的选择 $query="select Interval from Interval_Orar"; $result=mysqli_query($dbc,$query) or die("query failed: " . mysqli_error($dbc)); echo '<table>'; while ($row=mysqli_fetch_array($result)) { echo' <
选择,并在一个包含2列的表中显示该行。
这是我的选择
$query="select Interval from Interval_Orar";
$result=mysqli_query($dbc,$query) or die("query failed: " . mysqli_error($dbc));
echo '<table>';
while ($row=mysqli_fetch_array($result))
{
echo'
<tr>
<td>'.$row['Interval'].'</td>
</tr>';
}
echo '</table>';
$query=“从间隔中选择间隔”;
$result=mysqli_query($dbc,$query)或die(“查询失败:”.mysqli_error($dbc));
回声';
while($row=mysqli\u fetch\u数组($result))
{
回声'
“.$row['Interval']”
';
}
回声';
我要展示的是这样一张桌子
<tr>
<td>Interval 1</td>
<td>Interval 2</td>
</tr>
<tr>
<td>Interval 3</td>
<td>Interval 4</td>
</tr>
<tr>
<td>Interval 5</td>
<td>Interval 6</td>
</tr>
间隔1
间隔2
间隔3
间隔4
间隔5
间隔6
我不知道要做的是把2乘2选择放在while中,你可以在循环中做另一个mysqli\u fetch\u数组,检查是否有结果
并显示空单元格或包含内容的单元格。
然后在循环结束时,只需检查第二个mysqli\u fetch\u数组
是否有结果。(空单元格是可选的,这取决于您是否需要它,例如用于样式、添加而不是值
或其他内容)
将整个结果集转储到数组中,以便手动移动指针。
在你的循环中它将是(用简单的英语)
第1列=第1行
第2列=行1+1
行=行+1
重复。在第二列中,我仍然希望有$row['Interval']Thnx,第一个方法非常有效。如果我想放上3个collams,我只需要制作另一个mysqli\u fetch\u数组?我会在3分钟内接受你的回答:)是的,你可以这么做。但是您应该检查在上一次调用返回null
之后的另一次调用mysqli\u fetch\u array是否有效(因为我现在不知道,我确实添加了if(!$row){break;}
,这可能是可选的)@user3058067更新我的答案,但正如我在那里提到的,你需要小心,因为我现在无法测试它。
while ($row=mysqli_fetch_array($result))
{
echo'<tr>';
echo '<td>'.$row['Interval'].'</td>';
$row=mysqli_fetch_array($result);
if( $row ) {
echo '<td>'.$row['Interval'].'</td>';
} else {
echo '<td></td>';
}
echo '</tr>';
if( !$row ) {
break;
}
}
function createRows( $result, $columns ) {
if( $columns <= 0 ) {
return;
}
$row = true; //initialize it with true so that the first mysqli_fetch_array will be called
while($row !== null ) {
echo'<tr>';
for( $i=0 ; $i<columns ; $i++ ) {
if( $row !== null && $row=mysqli_fetch_array($result) ) {
echo '<td>'.$row['Interval'].'</td>';
} else {
echo '<td></td>';
}
}
echo '</tr>';
}
}