tags:

views:

80

answers:

5

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'); ?>
+5  A: 

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.

BoltClock
I think thats right but still didnt work. What about this statment? $sql = "SELECT * FROM user WHERE id = " . intval($userId);
tim
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.
BoltClock
Ok I turned error reporting on and Im getting Notice: Undefined variable: userid in
tim
I figured it out!
tim
@tim: be sure to accept whichever answer was most helpful to you by clicking the tick under the appropriate answer. Glad you solved it!
BoltClock
A: 

Remove the closing } on the last line.

Emil H
A: 

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.

Stoosh
+1  A: 

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'];
FreekOne
+2  A: 

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");
Brad F Jacobs
Is this set in the browser?
tim
@tim: You can set that in php.ini or to set it at runtime use the `error_reporting()` (http://www.php.net/manual/en/function.error-reporting.php) function.
FreekOne