PHP variable

by 4 replies
5
I wanted to fetch result from mysql database with the table name set to a session variable but it not working, here is the 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"


<?php
session_start();
$subject1=$_SESSION['subject1']='mathematics';

function getsubject1(){ global $subject1;

$questions=mysql_query("select * from $subject1 ") or die(mysql_error());
while($quest=mysql_fetch_array($question)){
echo $quest['question']; } }


?>
#programming #php #variable
  • See the correction I found out in your code:
    In the function you declaied, I see that you created a variable $questions
    but what you assigned to the variable $quest is $question (you did not add the ending "s" in subjects)

    Then ensure that there is a column is the database known as "question"

    <?php
    session_start();
    $subject1=$_SESSION['subject1']='mathematics';

    function getsubject1(){ global $subject1;

    $questions=mysql_query("select * from $subject1 ") or die(mysql_error());
    while($quest=mysql_fetch_array($questions)){
    echo $quest['question']; } }


    ?>



    et me know if it works this time.
  • At what point are you calling the function getsubject1?
    • [1] reply
    • Thanks guys, i have fixed the bug. I didn't pass the SESSION variable from the previous page, that was the problem. Thanks
  • Good you found the solution.

Next Topics on Trending Feed

  • 5

    I wanted to fetch result from mysql database with the table name set to a session variable but it not working, here is the 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"