Display results from DB using variables.

3 replies
Greetings all,
I am working on a computer based test application, where when the student logs in to test the test, the questions should come from the subjects the student was registered for.

I have tried using SESSION variable to hold the registered subjects and pass them to the query that will fetch the questions,but won't work.

here is a snippet my php code so far,
<?php


<?php

//confirm session
if(isset($_SESSION['user'])){ echo $_SESSION['user'];}

else{die("Session not started");}
//get student's details using session variables
$confirmuser=mysql_query("select * from students where username='$user' ") or die(mysql_error());
while($confirm=mysql_fetch_array($confirmuser)){ec ho"<br/>";
echo $confirm['surname'].'<br/>';
echo $confirm['firstname'].'<br/>';;
echo $_SESSION['subject1'] =$confirm['subject1'].'<br/>';
echo $_SESSION['subject2']=$confirm['subject2'].'<br/>';
echo $_SESSION['subject3']=$confirm['subject3'].'<br/>';
echo $_SESSION['subject4'] =$confirm['subject4'].'<br/>';





echo '<br/>';
$fourthsubject=$_SESSION['subject2'];

echo $fourthsubject. '<br/>';

function fourth(){ global $fourthsubject;
$bend=mysql_query("select * from $subject3 ") or die(mysql_error());
while($rase=mysql_fetch_array($bend)){
echo $rase['question'];}
;}

fourth();
}
?>



The above code is displaying this error message (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
' at line 1)
#display #results #variables
  • Profile picture of the author KirkMcD
    Originally Posted by heroecitadel View Post

    The above code is displaying error message.
    Don't leave us in suspense. What's the error?
    {{ DiscussionBoard.errors[8989584].message }}
  • Profile picture of the author KirkMcD
    echo $_SESSION['subject1'] =$confirm['subject1'].'<br/>';
    echo $_SESSION['subject2']=$confirm['subject2'].'<br/>';
    echo $_SESSION['subject3']=$confirm['subject3'].'<br/>';
    echo $_SESSION['subject4'] =$confirm['subject4'].'<br/>';
    Did you miss some quotes here? Possibly near the "="s and maybe some "."
    {{ DiscussionBoard.errors[8989594].message }}
  • Profile picture of the author Fanzi
    try this :
    <?php

    //confirm session
    if(isset($_SESSION['user'])){ $user=$_SESSION['user']; echo $_SESSION['user'];}

    else{die("Session not started");}

    //get student's details using session variables
    $confirmuser=mysql_query("select * from students where username='$user' ") or die(mysql_error());
    while($confirm=mysql_fetch_array($confirmuser)){ec ho"<br/>";
    echo'Here are the subjects you were registered for :<br/>';
    echo $confirm['surname'].'<br/>';
    echo $confirm['firstname'].'<br/>';;
    echo $_SESSION['subject1'] =$confirm['subject1'].'<br/>';
    echo $_SESSION['subject2']=$confirm['subject2'].'<br/>';
    echo $_SESSION['subject3']=$confirm['subject3'].'<br/>';
    echo $_SESSION['subject4'] =$confirm['subject4'].'<br/>';
    }


    echo '<br/>';

    $subject1=$_SESSION['subject1'];

    function subject(){global $love;
    $bend=mysql_query("select * from $subject1") or die(mysql_error());
    while($rase=mysql_fetch_array($bend)){
    echo $rase['question'];}
    ;}

    fourth();
    ?>
    {{ DiscussionBoard.errors[8989651].message }}

Trending Topics