使用PHP返回行的问题

时间:2009-12-04 16:45:46

标签: php mysql arrays rows

<?php

if (isset($_GET['flyerID']))
    $FlyerID = $_GET['flyerID'];

$DBConnect = @mysqli_connect("host", "UN", "pword")

    Or die("<p>Unable to connect to the datbase server.</p>" . "<p>Error Code ".mysqli_connect_errno().": ".mysqli_connect_error()) . "</p>";

$DBName = "agentsleuthdb";

@mysqli_select_db($DBConnect, $DBName)

    Or die("<p>Unable to select the database.</p>" . "<p>Error Code " . mysqli_errno($DBConnect) . ": " . mysqli_error($DBConnect)) ."</p>";

    $TableName = "FEEDBACK";

    $SQLstring = "SELECT * FROM $TableName order by FIRSTNAME";

    $QueryResult = @mysqli_query ($DBConnect, $SQLstring)

    Or die("<p> Unable to exequte Select query.</p>"."<p>Error Code ".mysqli_errno($DBConnect) .": ".mysqli_error

($DBConnect))."</p>";

    if (mysqli_num_rows($QueryResult) == 0){

        exit("<p>There is no feedback</p>");
}

?>


            <table border="1">
                <tr>
                    <th width = "15%">First Name </th>
                    <th width = "15%">Last Name </th>
                    <th width = "15%">Email Addr </th>
                    <th width = "15%">Company </th>
                    <th width = "40%">Feedback </th>

                </tr>


<?php
    $Row = mysqli_fetch_row($QueryResult);

do {
    echo "<tr><td>{$Row[0]}</td>";
    echo "<td>{$Row[1]}</td>";
    echo "<td>{$Row[2]}</td>";
    echo "<td>{$Row[3]}</td>";
    echo "<td>{$Row[4]}</td></tr>";
    $Row = mysqli_fetch_assoc($QueryResult);
} while ($Row);
mysqli_free_result($QueryResult);
mysqli_close($DBConnect);
?>              

它只返回一行..如何返回所有条目?

2 个答案:

答案 0 :(得分:3)

你试过吗

while ($Row = mysqli_fetch_row($QueryResult))

说明here

while ($Row = mysqli_fetch_array($QueryResult, MYSQL_ASSOC))

说明here

希望这有帮助。

答案 1 :(得分:0)

在您的代码中,只有您第一次调用mysqli_fetch_row时才会生成$Row索引数组。 这就是您在使用索引($Row$Row[0]等)访问$Row[1]内容时看到输出的原因。 之后,您在关联数组中转换mysqli_fetch_assoc的{​​{1}},因此使用输出索引访问$Row不再有效。

$Row循环替换为建议的第一个do ... while循环。