显示PHP中MSSQL数据库的所有结果

时间:2017-06-13 07:42:46

标签: php sql sql-server database

我正在尝试显示数据库中的所有结果,但它只显示最后结果,有人可以帮助我吗?

    <?php
            $serverName = "asdasd-DEMO"; 
            $options = array(  "UID" => "sa",  "PWD" => "#asdasfi",  "Database" => "demo");
            $conn = sqlsrv_connect($serverName, $options);
                if( $conn ) {
                echo "Connection established.";
                $sql="SELECT * FROM cliente";
                $aResult=sqlsrv_query($conn, $sql);
                while($rows = sqlsrv_fetch_array($aResult))
                { 
                $id = $rows['id'];
                $nome = $rows['nome'];
                $idade = $rows['idade'];
                }
            }else{
                 echo "Connection could not be established.<br />";
                 die( print_r( sqlsrv_errors(), true));
            }
?>
        <p>
            <td><b><font color='#663300'><?php echo $id;?></font></b></td>
            <td><b><font color='#663300'><?php echo $nome;?></font></b></td>
            <td><b><font color='#663300'><?php echo $idade;?></font></b></td>
        </p>

由于

2 个答案:

答案 0 :(得分:0)

你必须在你的循环中放置显示:

<?php
        $serverName = "asdasd-DEMO"; 
        $options = array(  "UID" => "sa",  "PWD" => "#asdasfi",  "Database" => "demo");
        $conn = sqlsrv_connect($serverName, $options);
            if( $conn ) {
            echo "Connection established.";
            $sql="SELECT * FROM cliente";
            $aResult=sqlsrv_query($conn, $sql);
            while($rows = sqlsrv_fetch_array($aResult))
            { 
            $id = $rows['id'];
            $nome = $rows['nome'];
            $idade = $rows['idade'];
?>
    <p>
        <td><b><font color='#663300'><?php echo $id;?></font></b></td>
        <td><b><font color='#663300'><?php echo $nome;?></font></b></td>
        <td><b><font color='#663300'><?php echo $idade;?></font></b></td>
    </p>
<?php
            }
        }else{
             echo "Connection could not be established.<br />";
             die( print_r( sqlsrv_errors(), true));
        }

答案 1 :(得分:0)

您需要存储结果中的每一行,并循环遍历它们。

    <?php
            $serverName = "asdasd-DEMO"; 
            $options = array(  "UID" => "sa",  "PWD" => "#asdasfi",  "Database" => "demo");
            $conn = sqlsrv_connect($serverName, $options);
            $results = array();
                if( $conn ) {
                echo "Connection established.";
                $sql="SELECT * FROM cliente";
                $aResult=sqlsrv_query($conn, $sql);
                while($rows = sqlsrv_fetch_array($aResult))
                { 
                    $results[] = $row;
                }
            }else{
                 echo "Connection could not be established.<br />";
                 die( print_r( sqlsrv_errors(), true));
            }
    foreach($results as $row) {
?>
        <p>
            <td><b><font color='#663300'><?php echo $row['id'];?></font></b></td>
            <td><b><font color='#663300'><?php echo $row['nome'];?></font></b></td>
            <td><b><font color='#663300'><?php echo $row['idade'];?></font></b></td>
        </p>
<?php
    }