Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have 2 tables
question(q_id,question,dept)
answer(a_id,q_id,answer)

I want that when i click on particular dept than it should show me question then its answer(can be more than one ) then next question tthen its answer like in forums but its not doing it is printing question then all anwers related to that dept not q_id
PHP
<?php
    $q=$_GET["q"];
    $host="localhost";
    $db_name="portal";
    $tb_name="question";
    $con=mysql_connect("$host","root","")or die("cannot connect");
    mysql_select_db("$db_name",$con)or die("cannot connect");
    $sql="SELECT question FROM question WHERE dept='".$q."'";
    //$sql2="SELECT q_id FROM question WHERE dept='".$q."'";
    $sql1="SELECT answer FROM answer WHERE q_id in (SELECT q_id FROM question WHERE dept='".$q."') " ;

    $result= mysql_query($sql);
    $result1= mysql_query($sql1);
    while($row = mysql_fetch_array($result) )
    {
        echo "<br>";
        echo "Q. ";
        echo $row[0];
        while($row1 = mysql_fetch_row($result1))
        {

        echo "<br>";
        echo "Ans.  ";

        echo $row1[0];
        }
    }
Posted
Updated 29-Sep-12 0:06am
v2
Comments
YoGiV 28-Sep-12 14:11pm    
I have 2 tables
question(q_id,question,dept)
answer(a_id,q_id,answer)

so i want that when i click on particular dept than it should show me question then its answer(can be more than one ) then next question tthen its answer like in forums but its not doing it is printing question then all anwers related to that dept not q_id
ZurdoDev 28-Sep-12 14:25pm    
I don't do PHP but you appear to have a loop within a loop. Just a guess, but that is likely why you are seeing everything.
fjdiewornncalwe 28-Sep-12 20:57pm    
That part is ok I think because result1 is his question and result2 is the answers to the question.
Sergey Alexandrovich Kryukov 28-Sep-12 14:57pm    
What do you want to achieve? What happened instead? Any other relevant information? Why this code dump?
--SA
fjdiewornncalwe 28-Sep-12 15:45pm    
You need to tell us what "proper result" means first. We cannot magically see into your brain for that information.

1 solution

Hmmm, I think you have some design problems. The question ID should describe the question and this value could be passed in the link, similarly to how you did with the dept-value ( it's a quite freaky thing to call 'q' something that describes 'dept', you can loose control very easy )

When you pass the ID to request a question the SQL-queries became more simple. One for getting the question text according to ID (you won't even need a loop) and an another for getting the answers for the question.

Edit:
I think the problem is with the second query and where it's used. The result of first query contains all questions in dept.
The second query's result contains all of the answers for these questions.

I think, that the second query should be executed within the first loop:

PHP
$sql="SELECT question,q_id FROM question WHERE dept='".$q."'";



$result= mysql_query($sql);
while($row = mysql_fetch_array($result) )
{
    echo "<br>";
    echo "Q. ";
    echo $row[0];
    $q_id=$row[1];
    $sql1="SELECT answer FROM answer WHERE q_id='".$q_id."'" ;
    $result1= mysql_query($sql1);
    while($row1 = mysql_fetch_row($result1))
    {

    echo "<br>";
    echo "Ans.  ";

    echo $row1[0];
    }
}


Another way could be using a join in the queries,so you can get all question-answer pairs as a result, but you must achieve, that each question should be displayed only once. So it's more simply to run multiple sql-queries.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 28-Sep-12 20:55pm    
My vote of 2. There is nothing wrong with his queries themselves, or the way he uses the IDs.
macika123 28-Sep-12 21:35pm    
After your comment I revised my solution, and you are right I had to improve ( by changing the queries, maybe I'm addicted to criticising queries :) ).

BTW, I like your method, that you comment why you rated.
fjdiewornncalwe 28-Sep-12 22:14pm    
+5 now. I think your revision nailed it on the head. I didn't see that myself until reading your edit. Well done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900