tags:

views:

33

answers:

3

I'm very new to MySQL so I need some help please,

I have a table called "posts", this table has the following columns:

id - this is auto increment and primary key
title
content

I have a PHP page where I want to display all the posts from the database table. I want the title to show first and then the post content followed by the title of the next post and it's post content and so on... However if I do this:

$select = mysql_query("SELECT * FROM posts ORDER BY id DESC");

while ($result = mysql_fetch_array($select))
{
   echo $result['title'] . "<br />";
   echo $result['content'];
}

Of course I get a list of all my post titles followed by all my post contents. In other words it's like this:

DogsCats

Are also known as canines.Are also known as felines.

Instead of:

Dogs

Are also known as canines

Cats

Are also known as felines

What would I need to do to fix this?

Many thanks.

A: 

What about

while ($result = mysql_fetch_array($select))
{
   echo $result['title'] . "<br />";
   echo $result['content'] . "<br /><br />";
}

You're missing a line break after your content, so first occurrence would be fine, but it'll merge first content with second title.

Rubens Farias
This worked thanks.
Daemon
A: 

Try the following for your loop:

while ($result = mysql_fetch_array($select))
{
   echo $result['title'] . "<br />" . $result['content'] . "<br />";
}

Alternatively:

while ($result = mysql_fetch_array($select))
{
   printf("%s <br /> %s <br />", $result['title'], $result['content']);
}
Nick Craver
A: 

You should tell PHP what kind of array you want (MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH).

Since you are using column names as array subscripts, you should use MYSQL_ASSOC or MYSQL_BOTH:

while ($result = mysql_fetch_array($select, MYSQL_ASSOC))

...
w3steve