0

I'm trying to get specific content from every single row in my MySQL database inside a div, however it only shows content from the row with the highest id value.

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$query = "SELECT * FROM articles ORDER BY id";
$result = mysql_query($query) or die(mysql_error());

$count = mysql_num_rows($result);

while($row = mysql_fetch_array($result))
{
    $title = $row['title'];
    $summary = $row['summary'];
}
?>

<div class="content">
    <h2 class="titlecss"><?php echo ($title)?></h2>
    <p class="customfont"><?php echo ($summary)?></p>
</div>

Could anyone explain what I'm doing wrong here? I've been browsing the internet for 2 hours but can't seem to find anything...

1
  • 4
    You need to put your <div> inside the while loop Commented Mar 19, 2013 at 13:58

2 Answers 2

2

You need to output each div inside the while loop:

while($row = mysql_fetch_array($result))
{
    $title = $row['title'];
    $summary = $row['summary'];
    ?>
    <div class="content">
    <h2 class="titlecss"><?php echo ($title)?></h2>
    <p class="customfont"><?php echo ($summary)?></p>
    </div>
    <?php
}
Sign up to request clarification or add additional context in comments.

1 Comment

That... is extremely weird. I did try this exact method, and it didn't work. Now it does. I must have made a typo or something. :-/ anyhow, thanks!
0

You should do like this:

while($row = mysql_fetch_array($result)) {
   echo "<div class=\"content\">";
   echo "<h2 class=\"titlecss\">" . $row['title'] . "</h2>";
   echo "<p class=\"customfont\">" . $row['summary'] .  "</p>";
   echo "</div>";
}

The way you are doing it, you store every value in the same variable. So you put the first value, then the second, over the first, then the next, over the previous, and finally you write once (the last value).

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.