0

我正在尝试使用其中的数据库数据创建非常简单的表。该表有两行,每行包含三条数据(三列)。我玩了几个小时,但无法理解它。我知道 $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>
4

2 回答 2

3

你想要你的数据库数据到一个表吗?:

<TABLE border="3" cellspacing="5" cellpadding="5">
<?php
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) //loop through every result set
{ ?>
   <TR>
     <?php foreach($row as $item) { //loop trough every cell in the row  ?>
    <TD><?= $item ?> </TD>
   <?php } ?>
    <TD><?php //you can add none column spefic data here like actions(links) or images ?> </TD>
  <TR>
    <?php 
   }
?>
于 2013-05-16T11:14:56.480 回答
1

终于完成了。并经过测试

    <table border="3" cellspacing="5" cellpadding="5">

            <?php
            $sql = "select * from deals";
    $res = mysql_query($sql);
    $num_rows = mysql_num_rows($res);

    if($num_rows > 0)
    {
         $i = 1;
         echo "<tr>";
        while($row = mysql_fetch_object($res))
        {
    ?>
                <td><?php echo  $row->deal_name ?> </td>
                <?php 
    if( $i++%3 == 0 )
    echo "</tr><tr>";
               }
echo "</tr>";
    }
            ?>
于 2013-05-16T11:46:51.203 回答