我正在尝试使用其中的数据库数据创建非常简单的表。该表有两行,每行包含三条数据(三列)。我玩了几个小时,但无法理解它。我知道 $row['BandName'] 需要每次循环循环时都会索引并增加,但不知道如何将索引附加到它。我试过这样的事情:$row['BandName'][$i]
但它只显示一个全名字母(显然这是不对的)任何帮助将不胜感激..
该表如下所示:
include 'db_constants.php';
$conn = sqlsrv_connect( $host, $cnct);
if( $conn == false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
else
$tsql = "SELECT * FROM bands";
$stmt = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
else
{
$recordcount = sqlsrv_num_rows( $stmt );//count the records
if($recordcount >0)
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{ ?>
<TR>
<TABLE border="3" cellspacing="5" cellpadding="5">
<?php for($i=0; $i<3; $i++) {?>
<TD align="center" valign="top">
<table border="0" cellspacing="2" cellpadding="0">
<tr align="center">
<td valign="bottom"><a href="details.php" target="_self"><img src="band/1.jpg" width="140" height="110" border="0"></a></td>
</tr>
<tr align="center">
<td class="greytablesm"> <?php echo $row['BandName'];?></td>
</tr>
</table>
</TD>
<?php }?>
<TR>
<?php }
else {echo "not found";}
}
?>
表结构是
BandID - int autoincrement
BandName - nvarchar(50)
BandBio - nvarchar(max)
Date - date
START TABLE <table>
loop from 1 to 2
<tr>
loop from 1 to 2
<td>
Show Details
</td>
end loop
</tr>
end loop
END TABLE </table>