Looping Through Database Query

Posted by DrakeNET on Stack Overflow See other posts from Stack Overflow or by DrakeNET
Published on 2011-06-27T06:06:05Z Indexed on 2011/06/27 8:22 UTC
Read the original article Hit count: 152

Filed under:
|
|

I am creating a very simple script. The purpose of the script is to pull a question from the database and display all answers associated with that particular question. We are dealing with two tables here and there is a foreign key from the question database to the answer database so answers are associated with questions.

Hope that is enough explanation. Here is my code. I was wondering if this is the most efficient way to complete this or is there an easier way?

<html>
<head>
<title>Advise Me</title>
<head>
    <body>

    <h1>Today's Question</h1>
    <?php 

    //Establish connection to database
    require_once('config.php');
    require_once('open_connection.php');

    //Pull the "active" question from the database
    $todays_question = mysql_query("SELECT name, question 
                                    FROM approvedQuestions
                                    WHERE status = active")
                                    or die(mysql_error());

    //Variable to hold $todays_question aQID
    $questionID = mysql_query("SELECT commentID FROM approvedQuestions
                                WHERE status = active")
                                or die(mysql_error());

    //Print today's question
    echo $todays_question;

    //Print comments associated with today's question
    $sql = "SELECT commentID FROM approvedQuestions WHERE status = active";
    $result_set = mysql_query($sql);
    $result_num = mysql_numrows($result_set);
    for ($a = 0; $a < $result_num; $a++)
        {
            echo $sql;
        }

    ?>


    </body>
</html>

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql