PHP 中的 While 循环问题

发布于 2024-09-18 07:26:52 字数 997 浏览 4 评论 0原文

我的 WHILE 循环有什么问题吗?

    <?php
        include('header.php');
        $manage = "current_page_item";
        include('nav.php');
        include('sidebar.php');
    ?>
    <div class="primary">
    <br/>
    <?php
    $userId = $_GET['id'];
    echo "<div class=\"item_list\">";
    $sql = "SELECT * FROM user WHERE id = " . intval($userId);
    $result = mysql_query($sql);
    while($item = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        echo "<b>Title: </b>" . $item['item'] . "<br/><b>Email: </b>" . $item['email'] . "<br/>";
        echo "<b>Price: </b>" . $item['price'] . "</b><br/><b>Category: </b>" . $item['category'] . "</b><br/> <b>Extra: </b>" . ($item['extra'] ."</b><br/><b>Date Listed: </b>". $item['date'];
    }
    echo "</div>";
?>
</div>
<?php include('footer.php'); ?>

Any suggestion of whats wrong with my WHILE Loop?

    <?php
        include('header.php');
        $manage = "current_page_item";
        include('nav.php');
        include('sidebar.php');
    ?>
    <div class="primary">
    <br/>
    <?php
    $userId = $_GET['id'];
    echo "<div class=\"item_list\">";
    $sql = "SELECT * FROM user WHERE id = " . intval($userId);
    $result = mysql_query($sql);
    while($item = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        echo "<b>Title: </b>" . $item['item'] . "<br/><b>Email: </b>" . $item['email'] . "<br/>";
        echo "<b>Price: </b>" . $item['price'] . "</b><br/><b>Category: </b>" . $item['category'] . "</b><br/> <b>Extra: </b>" . ($item['extra'] ."</b><br/><b>Date Listed: </b>". $item['date'];
    }
    echo "</div>";
?>
</div>
<?php include('footer.php'); ?>

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

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

发布评论

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

评论(5

一生独一 2024-09-25 07:26:52

你的错误就在这里。您使用了错误的变量名称来获取行:

while($userid = mysql_fetch_array($result, MYSQL_ASSOC)){

应该是:

while($item = mysql_fetch_array($result, MYSQL_ASSOC)){

此外,最后一行在结束标记 ?>} >。我不知道它是否是被您在问题中遗漏的开放块所孤立的,或者它确实是错误地存在的。

Your mistake is here. You're using the wrong variable name to fetch rows:

while($userid = mysql_fetch_array($result, MYSQL_ASSOC)){

It should be:

while($item = mysql_fetch_array($result, MYSQL_ASSOC)){

Additionally, there's a loose closing brace } at the very last line just before the closing tag ?>. I don't know if it was orphaned by an opening block you left out of your question, or it was really there by mistake.

趁微风不噪 2024-09-25 07:26:52

除了 BoltClock 所说的和 stoosh 之外,您还遇到了语法错误:

echo "<b>Price: </b>" . $item['price'] .
     "</b><br/> <b>Category: </b>". $item['category'] . 
     "</b><br/> <b>Extra: </b>". $item['extra'] . 
     "</b><br/><b>Date Listed: </b>". $item['date'];

您有两个没有任何意义的 paran,而我的赌注会导致语法错误。您确实应该将 error_reporting 设置为 E_ALL 并将 display_errors 设置为 on 以进行开发!它使调试这些东西变得更加容易。

更新

要为脚本临时设置它,请将其添加到顶部(当然在 之后)

error_reporting(E_ALL);
ini_set("display_errors", "on");

Along with what BoltClock said and stoosh, you also have a syntax error:

echo "<b>Price: </b>" . $item['price'] .
     "</b><br/> <b>Category: </b>". $item['category'] . 
     "</b><br/> <b>Extra: </b>". $item['extra'] . 
     "</b><br/><b>Date Listed: </b>". $item['date'];

You had two parans where they did not make any sense, and my bet cause a syntax error. You really should have error_reporting set to E_ALL and display_errors set to on for development! It makes debugging this stuff a ton easier.

Update

To set that up temporary for a script add this to the top (after <?php of course)

error_reporting(E_ALL);
ini_set("display_errors", "on");
心如狂蝶 2024-09-25 07:26:52

在第二条回显线上,有一些杂散的括号。应该是:

echo "<b>Price: </b>" . $item['price'] . "</b><br/> <b>Category: </b>" . $item['category'] . "</b><br/> <b>Extra: </b>" . $item['extra'] . "</b><br/><b>Date Listed: </b>" . $item['date'];

On the second echo line, you have a few stray parentheses. Sould be:

echo "<b>Price: </b>" . $item['price'] . "</b><br/> <b>Category: </b>" . $item['category'] . "</b><br/> <b>Extra: </b>" . $item['extra'] . "</b><br/><b>Date Listed: </b>" . $item['date'];
白况 2024-09-25 07:26:52

看来你的变量命名错误了?

您已在 while 函数参数中传递了 $userid ,但在循环中使用了 $item

除非您只发布了函数的片段,否则您还会获得额外的 }

Seems like you've misnamed your variables?

You've passed $userid in your while function argument but you're using $item in your loop?

You've also got an extra } unless you've only posted a snippet of a function.

蒲公英的约定 2024-09-25 07:26:52

删除最后一行的结束 }

Remove the closing } on the last line.

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