PHP 中的循环不工作

发布于 2024-11-25 01:51:40 字数 516 浏览 0 评论 0原文

$qPhysician = mysql_query("SELECT * FROM physicians");
$num = mysql_num_rows($qPhysician);
$i=0;
while($i < $num)
{
    "<tr>";
    "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";
    "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";
    "</tr>";
    $i++;
}

我得到空白结果。

如果我回显 $num,我会得到“19”,这是我的数据库中的行数。 如果我 echo $rowPhysician['lastName'] 只是为了测试是否获取记录,那么我至少会获取 1 条姓氏记录。不知道“while”有没有问题。请帮帮我。

$qPhysician = mysql_query("SELECT * FROM physicians");
$num = mysql_num_rows($qPhysician);
$i=0;
while($i < $num)
{
    "<tr>";
    "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";
    "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";
    "</tr>";
    $i++;
}

I get blank result.

If I echo $num, I get "19" which is the number of rows in my DB.
If I echo $rowPhysician['lastName'] just to test out if I get records, I get at least 1 record of last name. I don't know if there is something wrong with "while". Please help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-12-02 01:51:40

您是否缺少打印字符串的回声?

while($i < $num) {
    echo "tr";
    echo "td" . mysql_result($qPhysician,$i,"lastName") . "/td";
    echo "td" . mysql_result($qPhysician,$i,"firstName") . "/td";
    echo "/tr";
    $i++;
}

Are you missing the echo to print out the strings?

while($i < $num) {
    echo "tr";
    echo "td" . mysql_result($qPhysician,$i,"lastName") . "/td";
    echo "td" . mysql_result($qPhysician,$i,"firstName") . "/td";
    echo "/tr";
    $i++;
}
独﹏钓一江月 2024-12-02 01:51:40

从技术上讲,这不是一个答案,但以这种方式显示代码更容易:

// first, only take the records you need -- each record adds more time to
// query, so if you only need 2, only select 2.
$query = mysql_query("SELECT lastName, firstName FROM physicians");

// mysql_fetch_assoc returns an associative array of all of the columns
// mysql_fetch_row   returns a numerically indexed array.
// mysql_fetch_array returns an array with both numeric and string indexing.
// they will all return FALSE when there are no more results in the query.
while( $arr = mysql_fetch_assoc( $query ) )
{
    echo "<tr>";
    // Now, use array indexing.
    echo "<td>" . $arr[ "lastName" ] . "</td>";
    echo "<td>" . $arr[ "firstName" ] . "</td>";
    echo "</tr>";
}

在极少数情况下,mysql_result实际上是更好的选择——通常最好在 PHP 中填充几个数组,然后完成数据库。

Technically, this isn't an answer, but it is easier to show the code this way:

// first, only take the records you need -- each record adds more time to
// query, so if you only need 2, only select 2.
$query = mysql_query("SELECT lastName, firstName FROM physicians");

// mysql_fetch_assoc returns an associative array of all of the columns
// mysql_fetch_row   returns a numerically indexed array.
// mysql_fetch_array returns an array with both numeric and string indexing.
// they will all return FALSE when there are no more results in the query.
while( $arr = mysql_fetch_assoc( $query ) )
{
    echo "<tr>";
    // Now, use array indexing.
    echo "<td>" . $arr[ "lastName" ] . "</td>";
    echo "<td>" . $arr[ "firstName" ] . "</td>";
    echo "</tr>";
}

It is a very rare circumstance that mysql_result is actually a better option -- usually it is better to just populate a couple of arrays in PHP and be done with the DB.

凉月流沐 2024-12-02 01:51:40

不管怎样,回显 HTML 是不好的做法。更正确的输出应该类似于

...
while($i < $num) :?>
    <tr>
      <td><?php echo mysql_result($qPhysician,$i,"lastName"); ?></td>
      <td><?php echo mysql_result($qPhysician,$i,"firstName") ?></td>
    </tr>
<?php $i++; endwhile;
...

注意,您也写了错误的关闭 HTML。

Anyways echo'ing HTML is bad practice. The more correct output could should look like

...
while($i < $num) :?>
    <tr>
      <td><?php echo mysql_result($qPhysician,$i,"lastName"); ?></td>
      <td><?php echo mysql_result($qPhysician,$i,"firstName") ?></td>
    </tr>
<?php $i++; endwhile;
...

Note that you wrote closing HTML wrong, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文