Page 1 of 1

Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 12:35 pm
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 )

Re: Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 12:42 pm
by Celauran
What does your table schema look like?

Re: Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 1:05 pm
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.

Re: Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 1:21 pm
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

Re: Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 1:23 pm
by merofelde
Celauran wrote:What does your table schema look like?
the table contains the following fields
id, question, reply

that's it.

Re: Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 1:26 pm
by Celauran
Aside from an extra closing parenthesis, it looks like it should work. Can you elaborate on 'not working'?

Re: Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 2:11 pm
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

Re: Displaying data from a mysql table in php

Posted: Tue Feb 07, 2012 3:47 pm
by merofelde
fixed the closing paren. and now its working > DOH!!! I feel like a butt nugget LoL
Thanks guys.