Tim
Tim

Reputation: 783

Problem with While Loop in 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'); ?>

Upvotes: 0

Views: 281

Answers (5)

Emil H
Emil H

Reputation: 40230

Remove the closing } on the last line.

Upvotes: 0

Jim
Jim

Reputation: 18863

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");

Upvotes: 2

Valentin Flachsel
Valentin Flachsel

Reputation: 10825

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'];

Upvotes: 1

Stoosh
Stoosh

Reputation: 2429

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.

Upvotes: 0

BoltClock
BoltClock

Reputation: 724552

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.

Upvotes: 5

Related Questions