Jump to content

"Parse error: syntax error, unexpected T_CLASS" (PHP)

Hi guys!

 

I am building  a little CMS and i get this really stupid error: "Parse error: syntax error, unexpected T_CLASS on line 3".

The code for this file (simpleCMS.php) is: 

<? php    class simpleCMS{        var $host;    var $username;    var $password;    var $table;        public function display_public() {            $q = "SELECT * FROM testDB ORDER BY created DESC LIMIT 3";            $r = mysql_query($q);        if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display .= <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>        ENTRY_DISPLAY;      }                }        else {        $entry_display = <<<ENTRY_DISPLAY        <h2>This Page Is Under Construction</h2>        <p>            No entries have been made on this page.             Please check back soon, or click the            link below to add an entry!        </p>        ENTRY_DISPLAY;        }    $entry_display .= <<<ADMIN_OPTION        <p class="admin_link">            <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>        </p>    ADMIN_OPTION;        return $entry_display;}    public function display_admin() {            return <<<ADMIN_FORM                <form action="{$_SERVER['PHP_SELF']}" method="post">                    <label for="title">Titel:</label>                    <input name="title" id="title" type="text" maxlength="150" />                    <label for="bodytext">Bericht:</label>                    <textarea name="bodytext" id="bodytext"></textarea>                    <input type="submit" value="Verstuur!" />                </form>                ADMIN_FORM;}    public function write($p) {            if ( $p['title'] )            $title = mysql_real_escape_string($p['title']);            if ( $p['bodytext'])            $bodytext = mysql_real_escape_string($p['bodytext']);            if ( $title && $bodytext ) {            $created = time();            $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";            return mysql_query($sql);        }             else {                return false;                }}            public function connect() {            mysql_connect($this->host,$this->username,$this->password) or die("kon niet verbinden met de database. " . mysql_error());            mysql_select_db($this->table) or die("Weet je zeker dat die database wel bestaat?. " . mysql_error());            return $this->buildDB();}            private function buildDB() {            $sql = <<<MySQL_QUERY            CREATE TABLE IF NOT EXISTS testDB (                title	VARCHAR(150),                bodytext	TEXT,                created	VARCHAR(100)            )                MySQL_QUERY;            return mysql_query($sql);        }} ?>

And i have another file (display.php) and i get the same error. (i changed the login-credentials for obvious reasons) The code is:

<!DOCTYPE html><html lang="en">  <head>    <title>SimpleCMS</title>  </head>  <body><?php  include_once('simpleCMS.php');  $obj = new simpleCMS();  $obj->host = '#';  $obj->username = '#';  $obj->password = '#';  $obj->table = '#';  $obj->connect();  if ( $_POST )    $obj->write($_POST);  echo ( $_GET['admin'] == 1 ) ? $obj->display_admin() : $obj->display_public();?>  </body></html>

What seems to be causing the problem? I can't fingure it out! 

[CPU: AMD FX-6100 @3.3GHz ] [MoBo: Asrock 970 Extreme4] [GPU: Gigabyte 770 OC ] [RAM: 8GB] [sSD: 64gb for OS] [PSU: 550Watt Be Quiet!] [HDD: 1TB] [CPU cooler: Be Quiet! Shadow Rock Pro Sr1]  -Did i solve your question/problem? Please click 'Marked Solved'-

Link to post
Share on other sites

Well, you've come a long way since you posted on here last... I removed like 4 errors, but I seriously cannot get the final one- An end of file error. Everything looks to be fine. All brackets are closed, semicolons are there, no random quotes. You might have to play with this some more. I might just be missing something... Heredocs seriously screw with my brain for some reason... Anyways, commented code for your perusal:

 

<?php //As mentioned, fix the opening tag    class simpleCMS{        var $host;    var $username;    var $password;    var $table;        public function display_public() {            $q = "SELECT * FROM testDB ORDER BY created DESC LIMIT 3";            $r = mysql_query($q);        if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display = <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>        ENTRY_DISPLAY;      }                }        else {        $entry_display = <<<ENTRY_DISPLAY        <h2>This Page Is Under Construction</h2>        <p>            No entries have been made on this page.             Please check back soon, or click the            link below to add an entry!        </p>        ENTRY_DISPLAY;        }    $entry_display .= <<<ADMIN_OPTION        <p class="admin_link">            <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>        </p>    ADMIN_OPTION;        return $entry_display;}/*== THE ENTRY DISPLAY FIXES ==In your if/else statement you were appending to $entry_display when entry_display might not existEXAMPLE: $entry_display .= <<<ENTRY_DISPLAY - At this point entry_display didn't exist, so this would throw an error. So I changed ".=" to just "=". You did get this right in the else though. Then under the if else statement $entry_display would have been initialised either way so it's fine to append ADMIN_OPTION there. Finally, you had white space after you closed your heredoc. This isn't allowed, so I just moved return $entry_display; up 1 line*/   public function display_admin() {            return <<<ADMIN_FORM                <form action="{$_SERVER['PHP_SELF']}" method="post">                    <label for="title">Titel:</label>                    <input name="title" id="title" type="text" maxlength="150" />                    <label for="bodytext">Bericht:</label>                    <textarea name="bodytext" id="bodytext"></textarea>                    <input type="submit" value="Verstuur!" />                </form>                ADMIN_FORM;}public function write($p) {            if ( $p[title] )//removed single quotes from all of these... Trust me            $title = mysql_real_escape_string($p[title]);            if ( $p[bodytext] )            $bodytext = mysql_real_escape_string($p[bodytext]);            if ( $title && $bodytext ) {            $created = time();            $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";            return mysql_query($sql);        }             else {                return false;                }}            public function connect() {            mysql_connect($this->host,$this->username,$this->password) or die("kon niet verbinden met de database. " . mysql_error());            mysql_select_db($this->table) or die("Weet je zeker dat die database wel bestaat?. " . mysql_error());            return $this->buildDB();}            private function buildDB() {            $sql = <<<MySQL_QUERY            CREATE TABLE IF NOT EXISTS testDB (                title	VARCHAR(150),                bodytext	TEXT,                created	VARCHAR(100)            )                MySQL_QUERY;            return mysql_query($sql);//Again, moved this up a line to removed whitespace after heredoc        }}?> 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

Hazy125, on 11 Feb 2015 - 10:32 PM, said:Hazy125, on 11 Feb 2015 - 10:32 PM, said:Hazy125, on 11 Feb 2015 - 10:32 PM, said:Hazy125, on 11 Feb 2015 - 10:32 PM, said:

Well, you've come a long way since you posted on here last... I removed like 4 errors, but I seriously cannot get the final one- An end of file error. Everything looks to be fine. All brackets are closed, semicolons are there, no random quotes. You might have to play with this some more. I might just be missing something... Heredocs seriously screw with my brain for some reason... Anyways, commented code for your perusal:

 

<?php //As mentioned, fix the opening tag    class simpleCMS{        var $host;    var $username;    var $password;    var $table;        public function display_public() {            $q = "SELECT * FROM testDB ORDER BY created DESC LIMIT 3";            $r = mysql_query($q);        if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display = <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>        ENTRY_DISPLAY;      }                }        else {        $entry_display = <<<ENTRY_DISPLAY        <h2>This Page Is Under Construction</h2>        <p>            No entries have been made on this page.             Please check back soon, or click the            link below to add an entry!        </p>        ENTRY_DISPLAY;        }    $entry_display .= <<<ADMIN_OPTION        <p class="admin_link">            <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>        </p>    ADMIN_OPTION;        return $entry_display;}/*== THE ENTRY DISPLAY FIXES ==In your if/else statement you were appending to $entry_display when entry_display might not existEXAMPLE: $entry_display .= <<<ENTRY_DISPLAY - At this point entry_display didn't exist, so this would throw an error. So I changed ".=" to just "=". You did get this right in the else though. Then under the if else statement $entry_display would have been initialised either way so it's fine to append ADMIN_OPTION there. Finally, you had white space after you closed your heredoc. This isn't allowed, so I just moved return $entry_display; up 1 line*/   public function display_admin() {            return <<<ADMIN_FORM                <form action="{$_SERVER['PHP_SELF']}" method="post">                    <label for="title">Titel:</label>                    <input name="title" id="title" type="text" maxlength="150" />                    <label for="bodytext">Bericht:</label>                    <textarea name="bodytext" id="bodytext"></textarea>                    <input type="submit" value="Verstuur!" />                </form>                ADMIN_FORM;}public function write($p) {            if ( $p[title] )//removed single quotes from all of these... Trust me            $title = mysql_real_escape_string($p[title]);            if ( $p[bodytext] )            $bodytext = mysql_real_escape_string($p[bodytext]);            if ( $title && $bodytext ) {            $created = time();            $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";            return mysql_query($sql);        }             else {                return false;                }}            public function connect() {            mysql_connect($this->host,$this->username,$this->password) or die("kon niet verbinden met de database. " . mysql_error());            mysql_select_db($this->table) or die("Weet je zeker dat die database wel bestaat?. " . mysql_error());            return $this->buildDB();}            private function buildDB() {            $sql = <<<MySQL_QUERY            CREATE TABLE IF NOT EXISTS testDB (                title	VARCHAR(150),                bodytext	TEXT,                created	VARCHAR(100)            )                MySQL_QUERY;            return mysql_query($sql);//Again, moved this up a line to removed whitespace after heredoc        }}?> 

With C# an "end of file" error usually (in my experience anyway) means that there are too many brackets. Try removing one bracket from the last line (doesn't look right but give it a try)

 

Edit: Try changing:

 if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display .= <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>        ENTRY_DISPLAY;      }                }        else {        $entry_display = <<<ENTRY_DISPLAY

to:

 if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display .= <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>        ENTRY_DISPLAY;      }                    else {        $entry_display = <<<ENTRY_DISPLAY
Link to post
Share on other sites

 

With C# an "end of file" error usually (in my experience anyway) means that there are too many brackets. Try removing one bracket from the last line (doesn't look right but give it a try)

 

Edit: Try changing:

 if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display .= <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>        ENTRY_DISPLAY;      }                }        else {        $entry_display = <<<ENTRY_DISPLAY

to:

 if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display .= <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>        ENTRY_DISPLAY;      }                    else {        $entry_display = <<<ENTRY_DISPLAY

Nah, that won't work. Leaves the if statement open. The problem is the heredocs, that is, all of the things that have "<<<" in front of them. They're a special way to write strings in php that passes all variables, and quotations so that you don't have to worry about escaping quotes and concatenation of variables.

 

@Jeroen1322 I did fix it, but the solution is less than desirable, I replace all heredocs with just double quotes and manually escaped the required things. Not really elegant, but as mentioned above, heredocs do my head in so I couldn't really find an easier solution 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

Building on Hazys fixes.

<?php //As mentioned, fix the opening tag    class simpleCMS{        var $host;    var $username;    var $password;    var $table;        public function display_public() {            $q = "SELECT * FROM testDB ORDER BY created DESC LIMIT 3";            $r = mysql_query($q);        if ( $r !== false && mysql_num_rows($r) > 0 ) {        while ( $a = mysql_fetch_assoc($r) ) {            $title = stripslashes($a['title']);            $bodytext = stripslashes($a['bodytext']);            $entry_display = <<<ENTRY_DISPLAY            <h2>$title</h2>            <p>                $bodytext            </p>ENTRY_DISPLAY; // Do not indent this      }                }        else {        $entry_display = <<<ENTRY_DISPLAY        <h2>This Page Is Under Construction</h2>        <p>            No entries have been made on this page.             Please check back soon, or click the            link below to add an entry!        </p>ENTRY_DISPLAY; // Do not indent this        }    $entry_display .= <<<ADMIN_OPTION        <p class="admin_link">            <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>        </p>ADMIN_OPTION; // Do not indent this        return $entry_display;}/*== THE ENTRY DISPLAY FIXES ==In your if/else statement you were appending to $entry_display when entry_display might not existEXAMPLE: $entry_display .= <<<ENTRY_DISPLAY - At this point entry_display didn't exist, so this would throw an error. So I changed ".=" to just "=". You did get this right in the else though. Then under the if else statement $entry_display would have been initialised either way so it's fine to append ADMIN_OPTION there. Finally, you had white space after you closed your heredoc. This isn't allowed, so I just moved return $entry_display; up 1 line*/   public function display_admin() {            return <<<ADMIN_FORM                <form action="{$_SERVER['PHP_SELF']}" method="post">                    <label for="title">Titel:</label>                    <input name="title" id="title" type="text" maxlength="150" />                    <label for="bodytext">Bericht:</label>                    <textarea name="bodytext" id="bodytext"></textarea>                    <input type="submit" value="Verstuur!" />                </form>ADMIN_FORM; // Do not indent this}public function write($p) {            if ( $p[title] )//removed single quotes from all of these... Trust me            $title = mysql_real_escape_string($p[title]);            if ( $p[bodytext] )            $bodytext = mysql_real_escape_string($p[bodytext]);            if ( $title && $bodytext ) {            $created = time();            $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";            return mysql_query($sql);        }             else {                return false;                }}            public function connect() {            mysql_connect($this->host,$this->username,$this->password) or die("kon niet verbinden met de database. " . mysql_error());            mysql_select_db($this->table) or die("Weet je zeker dat die database wel bestaat?. " . mysql_error());            return $this->buildDB();}            private function buildDB() {            $sql = <<<MySQL_QUERY            CREATE TABLE IF NOT EXISTS testDB (                title	VARCHAR(150),                bodytext	TEXT,                created	VARCHAR(100)            )MySQL_QUERY; // Do not indent this            return mysql_query($sql);//Again, moved this up a line to removed whitespace after heredoc        }}?> 

Not a fan of HEREDOCs either, but I fixed your indentation, so they shouldn't cause an issue anymore.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×