Displaying data from a mysql table in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
merofelde
Forum Newbie
Posts: 9
Joined: Wed Oct 26, 2011 1:12 pm

Displaying data from a mysql table in php

Post by merofelde »

I have a php form that asks a question and when submitted will store the question in a MySql table called 'questions'.
I have another php form where an answer can be posted to that question and is stored in same table as the question.
How would I display all of the questions - those with answers and those without answers on a seperate page where the answer is beneath the question.

In the table I have the folowing fields
id,
question,
reply

Below is an example of how I would like the display to look. Some questions may not have answers while others will.

example:
Q1: How big is the car?
Q1Answer: It is 5 meters from bumper to bumper.

Q2: What color is the dog?

Q3: Will it rain today?
Q3Answer: Only if there are clouds in the sky.

Q4: Who is on first?

Q5: What time is it?

Q6: Do you have ice cream?
Q6Answer: We have chocolate and vanillia.




I have a basic idea on how to do this but am not sure of how to write the code for it. The idea would go something like this:

Check database for questions
---if there are questions
------print question
--------check database for answer to question
-----------if question has answer
-------------print answer
-----------else print blank line
---else print blank line
next check

If someone could give me hand writing the php for this I would be eternally indebted to them. (I'm a poor fellow and am already eternally in debt to many not the least of which is the bank LoL, but you will also have my eternal gratitude too. :D )
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Displaying data from a mysql table in php

Post by Celauran »

What does your table schema look like?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Displaying data from a mysql table in php

Post by mikosiko »

In the table I have the folowing fields
id,
question,
reply
if that is your table schema the code is extremely easy, and you should be able to write it without any problem... just one query to the table and a loop... what have you tried so far?... post the code that you have and your specific doubts.
merofelde
Forum Newbie
Posts: 9
Joined: Wed Oct 26, 2011 1:12 pm

Re: Displaying data from a mysql table in php

Post by merofelde »

Code: Select all


$result = mysql_query("SELECT * FROM question");

while ($row = mysql_fetch_array($result)))
{
echo "<div>" . $row['id'] . $row['question'] . "</div>";
echo "<div>". $row['reply'] . "</div>";


This is what I have and it is not working.

My code is weak as I am a young PHP padawan
merofelde
Forum Newbie
Posts: 9
Joined: Wed Oct 26, 2011 1:12 pm

Re: Displaying data from a mysql table in php

Post by merofelde »

Celauran wrote:What does your table schema look like?
the table contains the following fields
id, question, reply

that's it.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Displaying data from a mysql table in php

Post by Celauran »

Aside from an extra closing parenthesis, it looks like it should work. Can you elaborate on 'not working'?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Displaying data from a mysql table in php

Post by mikosiko »

merofelde wrote:

Code: Select all


$result = mysql_query("SELECT * FROM question");

while ($row = mysql_fetch_array($result)))
{
echo "<div>" . $row['id'] . $row['question'] . "</div>";
echo "<div>". $row['reply'] . "</div>";


This is what I have and it is not working.

My code is weak as I am a young PHP padawan
other than the extra closing parenthesis that Celauran pointed out...
is this your complete code? or we have to assume that you are already connected to the DB, and that you are closing the open { in some place ?

just 'not working' doesn't contribute to help you
merofelde
Forum Newbie
Posts: 9
Joined: Wed Oct 26, 2011 1:12 pm

Re: Displaying data from a mysql table in php

Post by merofelde »

fixed the closing paren. and now its working > DOH!!! I feel like a butt nugget LoL
Thanks guys.
Post Reply