jgkgopi Posted April 29, 2011 Share Posted April 29, 2011 hi all am new to this forum help me to overcome from this error Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\wamp\www\quiz1\quiz1.php on line 12 <?php include("contentdb.php"); $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); if (!$submit) { echo "<form method=post action=$_SERVER['PHP_SELF']>"; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; $answer = $row["answer"]; echo "<tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>"; } echo "</table>"; echo "<input type='submit' value='See how you did' name='submit'>"; echo "</form>"; } elseif ($submit) { $score = 0; $total = mysql_num_rows($display); while ($result = mysql_fetch_array($display)) { $answer = $result["answer"]; $q = $result["q"]; if ($$q == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; if ($score == $total) { echo "Congratulations! You got every question right!"; } elseif ($score/$total < 0.34) { echo "Oh dear. Not the best score, but don't worry, it's only a quiz."; } elseif ($score/$total > 0.67) { echo "Well done! You certainly know your stuff."; } else { echo "Not bad - but there were a few that caught you out!"; } echo "</p>"; echo "<p>Here are the answers:"; echo "<table border=0>"; $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); while ($row = mysql_fetch_array($display)) { $question = $row["question"]; $answer = $row["answer"]; $q = $row["q"]; echo "<tr><td><br>$question</td></tr>"; if ($$q == $answer) { echo "<tr><td>»you answered ${$q}, which is correct</td></tr>"; } elseif ($$q == "") { echo "<tr><td>»you didn't select an answer. The answer is $answer</td></tr>"; } else { echo "<tr><td>»you answered ${$q}. The answer is $answer</td></tr>"; } } echo "</table></p>"; } ?> thanks in adavance Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/ Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 can you show the line that is throwing the error please? Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208122 Share on other sites More sharing options...
Pikachu2000 Posted April 29, 2011 Share Posted April 29, 2011 Don't use $_SERVER['PHP_SELF'] as a form action. This also fixes the parse error. echo '<form method=post action="">'; Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208125 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 if you used __FILE__ as the action would it trigger an error? Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208136 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 __FILE__ returns an absolute filesystem path (eg /home/website/public/filename.php). That should never be used as the form action! Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208142 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 thanks wildteen...was just curious Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208143 Share on other sites More sharing options...
jgkgopi Posted April 29, 2011 Author Share Posted April 29, 2011 this is the line showing error echo "<form method=post action=$_SERVER['PHP_SELF']>"; Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208197 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 is your problem fixed? Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208199 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 this is the line showing error echo "<form method=post action=$_SERVER['PHP_SELF']>"; Did you read Pikachu2000's reply earlier Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1208200 Share on other sites More sharing options...
jgkgopi Posted May 2, 2011 Author Share Posted May 2, 2011 If i changed the code like this echo '<form method=post action="">'; It is showing error in this line. mysql_query("SELECT * FROM $table ORDER BY id",$db); Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209339 Share on other sites More sharing options...
PHPSuperNewb Posted May 2, 2011 Share Posted May 2, 2011 you didn't define the variable $table if I'm correct? Then how can you select something from a table that doesn't exist ? unless you did that in contentdb.php ofcourse.. But I wouldn't really know :S which error are you recieving? and if it says undefined variable, then where did you define your $table variable? Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209341 Share on other sites More sharing options...
jgkgopi Posted May 2, 2011 Author Share Posted May 2, 2011 PHPSuperNewb i had include in the contentdb.php this is the error i am getting mysql_query() expects parameter 2 to be Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209353 Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 The second parameter for mysql_query should be a valid mysql link resource. Where is the variable $db being defined? Note a mysql link resource is only returned from mysql_connect() when a connection to the mysql server has been established. Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209378 Share on other sites More sharing options...
jgkgopi Posted May 2, 2011 Author Share Posted May 2, 2011 Now i didn't get any but i had got What is SQL Structure Query Lang Super query language slow query lang See how you did if i click the correct answer also it is in the same page i think i don't know what to give in the action in quiz1.php i have attached all code with it Config.php <?php $database = "quiz"; $user = "root"; $pass = ""; $hostname = "localhost"; $table = "quiz"; ?> contentdb.php <?php include("config.php"); $db = mysql_connect("$hostname", "$user", "$pass"); mysql_select_db("$database",$db); ?>[/code] Quiz1.php <?php include("contentdb.php"); include("config.php"); global $submit,$db; $display = mysql_query("SELECT * FROM $table ORDER BY id",$db)or die (mysql_error()); if (!$submit) { echo '<form method="post" action="">'; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; $answer = $row["answer"]; echo "<tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>"; } echo "</table>"; echo "<input type='submit' value='See how you did' name='submit'>"; echo "</form>"; } elseif ($submit) { $score = 0; $total = mysql_num_rows($display); while ($result = mysql_fetch_array($display)) { $answer = $result["answer"]; $q = $result["q"]; if ($$q == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; if ($score == $total) { echo "Congratulations! You got every question right!"; } elseif ($score/$total < 0.34) { echo "Oh dear. Not the best score, but don't worry, it's only a quiz."; } elseif ($score/$total > 0.67) { echo "Well done! You certainly know your stuff."; } else { echo "Not bad - but there were a few that caught you out!"; } echo "</p>"; echo "<p>Here are the answers:"; echo "<table border=0>"; $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); while ($row = mysql_fetch_array($display)) { $question = $row["question"]; $answer = $row["answer"]; $q = $row["q"]; echo "<tr><td><br>$question</td></tr>"; if ($$q == $answer) { echo "<tr><td>»you answered ${$q}, which is correct</td></tr>"; } elseif ($$q == "") { echo "<tr><td>»you didn't select an answer. The answer is $answer</td></tr>"; } else { echo "<tr><td>»you answered ${$q}. The answer is $answer</td></tr>"; } } echo "</table></p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209422 Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 Looks like the connection to mysql is failing for some reason, as $db is not passing a valid link resource. This will happen if mysql_connect does not connect to mysql properly. You should check to see if there is a connection. Change contentdb.php to <? include("config.php"); $db = mysql_connect("$hostname", "$user", "$pass"); if(!$db) { trigger_error('Connection to mysql failed. ' . mysql_error(), E_USER_ERROR); } mysql_select_db("$database",$db); ?> Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209434 Share on other sites More sharing options...
jgkgopi Posted May 2, 2011 Author Share Posted May 2, 2011 still same problem Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209477 Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 still same problem Helps if you provide more information to your problem. Tell us what the problem is and what you're script should be doing. Quote Link to comment https://forums.phpfreaks.com/topic/235072-parse-error-parse-error-expecting-t_string-or-t_variable-or-t_num_string/#findComment-1209481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.