0

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'); ?>
7
  • 5
    For reference, you shouldn't be editing the code in the question to reflect the answers. People looking at the question later won't see the original code that caused the problem unless they look at the revision history, and it's a pain to look at the revision history and the answers at the same time. Commented Sep 10, 2010 at 23:08
  • 2
    To other editors: be very, very sure you're not changing any of the actual syntax or meaning of the code when you're attempting to format it more cleanly. As cHao says, this may cause confusion with the answers. Commented Sep 10, 2010 at 23:10
  • @cHao i think the changes made were just formatting and not to fix the code itself Commented Sep 10, 2010 at 23:12
  • @Simon_Weaver: see the first revision, on which BoltClock...'s answer is based. Someone's "fixed" the code since then. Commented Sep 10, 2010 at 23:16
  • cHao's first comment refers to tim incorporating our answers into his own edit. My comment refers to the possibility of any reformatting potentially changing the meaning of the code. Commented Sep 10, 2010 at 23:17

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I think thats right but still didnt work. What about this statment? $sql = "SELECT * FROM user WHERE id = " . intval($userId);
What do you mean by 'didnt work'? Does it give you an error? Try $result = mysql_query($sql) or die(mysql_error()); and tell me if it says anything. Also make sure error reporting is fully on; place error_reporting(E_ALL); at the beginning of your script.
Ok I turned error reporting on and Im getting Notice: Undefined variable: userid in
@tim: be sure to accept whichever answer was most helpful to you by clicking the tick under the appropriate answer. Glad you solved it!
2

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

1 Comment

@tim: You can set that in php.ini or to set it at runtime use the error_reporting() (php.net/manual/en/function.error-reporting.php) function.
1

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

Comments

0

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.

Comments

0

Remove the closing } on the last line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.