【问题标题】:Problem with returning rows using PHP使用 PHP 返回行的问题
【发布时间】:2009-12-04 16:45:45
【问题描述】:
<?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);
?>              

它只返回一行.. 我怎样才能返回所有条目?

【问题讨论】:

  • 你为什么在一个地方使用mysqli_fetch_row()而在另一个地方使用mysqli_fetch_assoc()?看起来您也应该在第二种情况下使用mysqli_fetch_row()(在do 循环内)。

标签: php mysql arrays rows


【解决方案1】:

你试过了吗

while ($Row = mysqli_fetch_row($QueryResult))

描述here

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

描述here

希望这会有所帮助。

【讨论】:

    【解决方案2】:

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

    do ... while 循环替换为第一个 while 循环 aforloney 建议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多