Jump to content

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'


jgkgopi

Recommended Posts

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

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?

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.

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>";



}

?>

 

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.