-2

I am attempting to create a student exam result system using PHP and a MySQL database. I'm running into two problems:

  1. How can I make a database file for this?
  2. I'm running into the following error message; how can I resolve it?

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\s\display.php on line 12

This is my display.php code:
<?php
mysql_connect("WWW.EXAMPLE.COM", "USERNAME", "PASSWORD") or die("Connection Failed");
mysql_select_db("YOUR DATABASE NAME")or die("Connection Failed");

$name = $_POST['name'];
$query = "select * from YOURTABLENAME where REGISTERNUMBER = '$name'";
$result = mysql_query($query);

while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "$line['name']."<br />";
    echo "$line['subject1']."</div></td>"."<br />";
    echo "$line['subject2']."</div></td>"."<br />";
    echo "$line['subject3']."</div></td>"."<br />";
    echo "$line['subject4']."</div></td>"."<br />";
}

?>

This is my index.php

<html>
    <form method="post" name="display" action="isplay.php" />
        <b>Enter the register number:</b><br>
        <input type="text" name="name" />
        <input type="submit" value="Search" />
    </form>
 </html>
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64

3 Answers3

0

Check your quotes

 <?php
    mysql_connect("WWW.EXAMPLE.COM", "USERNAME", "PASSWORD") or die("Connection Failed");
    mysql_select_db("YOUR DATABASE NAME")or die("Connection Failed");
    $name = $_POST['name'];
    $query = "select * from YOURTABLENAME where REGISTERNUMBER = '$name'";
    $result = mysql_query($query);
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
    {
       echo $line['name']."<br />";
       echo $line['subject1']."</div></td>"."<br />";
       echo $line['subject2']."</div></td>"."<br />";
       echo $line['subject3']."</div></td>"."<br />";
       echo $line['subject4']."</div></td>"."<br />";
    }

    ?>
Eduardo Ortiz
  • 407
  • 5
  • 12
0

It is because your strings do not have matching opening and closing quotes:

echo "$line['name']."<br />";
echo "$line['subject1']."</div></td>"."<br />";
echo "$line['subject2']."</div></td>"."<br />";
echo "$line['subject3']."</div></td>"."<br />";
echo "$line['subject4']."</div></td>"."<br />";

Try this:

echo "{$line['name']}<br>";
echo "{$line['subject1']}</div></td><br>";
echo "{$line['subject2']}</div></td><br>";
echo "{$line['subject3']}</div></td><br>";
echo "{$line['subject4']}</div></td><br>";

The HTML that you are outputting is also incorrect. You should read a little about how tables work.

If you expect us to help you "Create Student Exam Result System in PHP with MYSQL Database" then you are asking too much. You should learn PHP and SQL like everyone else: Study it and use it.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
0

You've missed closing " after the row fetch variables. Follow:

<?php
mysql_connect("WWW.EXAMPLE.COM", "USERNAME", "PASSWORD") or die("Connection Failed");
mysql_select_db("YOUR DATABASE NAME")or die("Connection Failed");
//added to prevent sql injection
$name = mysql_real_escape_string($_POST['name']);
$query = "select * from YOURTABLENAME where REGISTERNUMBER = '$name'";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "$line['name']"."<br />";
echo "$line['subject1']"."</div></td>"."<br />";
echo "$line['subject2']"."</div></td>"."<br />";
echo "$line['subject3']"."</div></td>"."<br />";
echo "$line['subject4']"."</div></td>"."<br />";
}

?>

You do not need quotes around the PHP variables, but the above code will work too because the double quotes " forces any variable inside it to be parsed as a variable but beware this is not the case with single quotes '.

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
AyB
  • 11,609
  • 4
  • 32
  • 47
  • Using quotes around php variables is a good practice. Even if it works without quotes does not mean it is okay. I prefer to always have them - to make the code more clear and to speed up the performance. – Paul Denisevich Apr 13 '14 at 05:27
  • @PaulDenisevich Could you please elaborate on why it's not a good practice? In terms of readability, I prefer having the variable separated from the string (unless there are lots of variables included in the string) and as far as performance goes, checked it with microtime() and the no-quotes one gave me fewer microseconds. Maybe there's something I missed? – AyB Apr 13 '14 at 05:36
  • @PaulDenisevich No, putting quotes around variables is a preference. It has nothing to do with "good practice". As for it making the code more clear, that is debatable. Also, it has virtually no effect on performance, what-so-ever. – Sverri M. Olsen Apr 13 '14 at 05:37
  • It's good though to get in the habit of using {$...} in double quotes instead of only $..., for times where you need to insert the variable in a string where it's not obvious to PHP which part is the variable and which part is the string. If you want the best performance, use string concatenation with single quotes. – Paul Denisevich Apr 13 '14 at 05:46
  • @ICanHasCheezburger yes, for a small app it will not affect your speed, but for serious app with 100000+ loads per minute it will I guess. – Paul Denisevich Apr 13 '14 at 05:47