繁体   English   中英

将数据库中的数据显示到表中

[英]Display data from database into a table

我试图用数据库中的数据创建一个非常简单的表。 该表有两行,每行各包含三段数据(三列)..我现在玩了几个小时,但是无法理解..我知道$ 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>

您要将数据库数据存储到表中?:

<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 
   }
?>

终于完成了。 经过测试

    <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>";
    }
            ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM