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))
{
ec开发者_StackOverflow社区ho "<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'); ?>
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.
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");
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'];
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.
Remove the closing }
on the last line.
精彩评论