0

I have created one php class It gives me syntax error. Unexpected end of file. As I test on localhost it works well. But when I test on server it shows this error. My server is on linux platform and I am working on windows.

Contact.php

<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', '1');

require 'database.php';

class Contact
{

    private $unique_id,$card_name,$name,$telephone_no,$company_name,$department,$job_title,$home_address,$work_address,$user_id,$status;


    function Contact($unique_id,$card_name,$name,$telephone_no,$company_name,$department,$job_title,$home_address,$work_address,$user_id,$status)
    {

        $this->unique_id = $unique_id;
        $this->card_name = $card_name;
        $this->name = $name;
        $this->telephone_no = $telephone_no;
        $this->company_name = $company_name;
        $this->department = $department;
        $this->job_title = $job_title;
        $this->home_address = $home_address;
        $this->work_address = $work_address;
        $this->user_id = $user_id;
        $this->status = $status;
    }

    function createContact()
    {

        $database = new Database(ContactsConstants::DBHOST,ContactsConstants::DBUSER,ContactsConstants::DBPASS,ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();

        $stmt = $dbConnection->prepare("select * from contact where name=?");
        $stmt->execute(array($this->name));
        $rows = $stmt->rowCount();

        if($rows > 0)
        {
            $response = array("status"=>-3,"message"=>"contact exists.");
            return $response;
        }


        $stmt = $dbConnection->prepare("insert into contact(card_name,name,telephone_no,company_name,department,job_title,home_address,work_address,user_id,status) values(?,?,?,?,?,?,?,?,?,?)");
        $stmt->execute(array($this -> card_name,$this -> name,$this -> telephone_no,$this -> company_name,$this -> department,$this -> job_title, $this -> home_address,
            $this -> work_address,$this -> user_id,$this -> status));
        $rows = $stmt->rowCount();
        $Id = $dbConnection->lastInsertId();

        $stmt = $dbConnection->prepare("select * from contact where unique_id=?");
        $stmt->execute(array($Id));
        $contact = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if($rows < 1) {
            $response = array("status"=>-1,"message"=>"Failed to add contact., unknown reason");
            return $response;
        }
        else
        {
            $response = array("status"=>1,"message"=>"Contact created successfully.","contact"=>$contact);
            return $response;
        }

    }

    function getContacts()
    {
        $database = new Database(ContactsConstants::DBHOST,ContactsConstants::DBUSER,ContactsConstants::DBPASS,ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();

        $stmt = $dbConnection->prepare("SELECT contact.unique_id, contact.card_name, contact.name,contact.telephone_no,contact.company_name,contact.department,
                                        contact.job_title,contact.home_address,contact.work_address,contact.user_id,contact.status, Users.user_name, Users.user_id FROM contact INNER JOIN Users
                                        ON contact.user_id = Users.user_id WHERE contact.user_id = ?");
        $stmt->execute(array($this -> user_id));
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $contacts = array();

        if (count($rows) > 0) {

            foreach($rows as $row)
            {
                $contacts[] = $row;
            }

            $response = array("status" => 1, "message" => "Success", "contacts" => $contacts);
            return json_encode($response);
        }

        else {
            $response = array("status"=>-1,"message"=>"Contact list is empty");
            return json_encode($response);
        }
    }

    function updateContact()
    {
        $database = new Database(ContactsConstants::DBHOST,ContactsConstants::DBUSER,ContactsConstants::DBPASS,ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();

        $stmt = $dbConnection->prepare("UPDATE contact SET `card_name` = :card_name, `name` = :name, `telephone_no` = :telephone_no,`company_name` = :company_name, 
                                        `department` = :department, `job_title` = :job_title, `home_address` = :home_address, `work_address` = :work_address, `user_id` = :user_id, `status` = :status WHERE `unique_id` = :unique_id");

        $stmt->execute(array(':card_name' => $this -> card_name, ':name' => $this -> name,':telephone_no' => $this -> telephone_no,':company_name' => $this -> company_name,':department' => $this -> department,
            ':job_title' => $this -> job_title, ':home_address' => $this -> home_address,':work_address' => $this -> work_address,':user_id' => $this -> user_id, ':status' => $this -> status, ':unique_id' => $this -> unique_id));

        $count = $stmt->rowCount();

        if($count > 0) {
            $response = array("status"=>1,"message"=>"Contact Updated Successfully.","contact"=>$count);
            return $response;
        }
        else {
            $response = array("status"=>-1,"message"=>"Failed to update.");
            return $response;
        }
    }

    function deleteContact()
    {

        $database = new Database(ContactsConstants::DBHOST,ContactsConstants::DBUSER,ContactsConstants::DBPASS,ContactsConstants::DBNAME);
        $dbConnection = $database->getDB();

        $stmt = $dbConnection->prepare("select * from contact where `unique_id` =?");
        $stmt->execute(array($this->unique_id));
        $rows = $stmt->rowCount();

        if($rows == 0)
        {
            $response = array("status"=>-3,"message"=>"contact dose not exists.");
            return $response;
        }

        $stmt = $dbConnection->prepare("Delete from contact WHERE `unique_id` = :unique_id");

        $stmt->execute(array(":unique_id"=>$this->unique_id));

        $count = $stmt->rowCount();

        if($count > 0) {
            $response = array("status"=>1,"message"=>"Contact Deleted Successfully.","contact"=>$count);
            return $response;
        }
        else {
            $response = array("status"=>-1,"message"=>"Failed to delete.");
            return $response;
        }
    }
}
?>

Error:

<br />
<b>Parse error</b>:  syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in
<b>/var/www/html/contactsapi/Contact.php</b> on line
<b>128</b>
<br />

Line no 128 is -

$stmt = $dbConnection->prepare("select * from contact where `unique_id` =?"); //line 128

I am using this in getContacts.php

getContacts.php

    <?php

header("Content-type: application/json");

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', '1');

require 'Contact.php';

    $jsonText = file_get_contents('php://input');

    $json = json_decode($jsonText);
    $user_id = $json->user_id;

    $contact = new Contact("","","","","","","","","",$user_id,"");
    $response = $contact->getContacts();

    if ( $response == null ) {
        $response = json_encode(array("result" => -2, "message" => "Empty result"));
        echo $response;
    } else {
        echo $response;
    }
?>

I have gone through the How to solve syntax errors link. I found no changes to do. I have checked curly braces, start and end of the tags. Also checked for case sensitivity. But not getting what's wrong here.

Can anyone help with this please. Thank you..

Sid
  • 2,792
  • 9
  • 55
  • 111
  • There could be many causes, but start uploading, or copying the file again... perhaps it got truncated? Or it might just be another version than you think. Verify it has the exact content you think it should have. – KIKO Software Aug 27 '16 at 06:13
  • yes i did that too.. Deleted the file and uploaded again several times. But that too has no effect. :-( @KIKOSoftware – Sid Aug 27 '16 at 06:17
  • Your line 143 is my line 136, when I copy your code and put it in an editor. That cannot be right? – KIKO Software Aug 27 '16 at 06:20
  • I have deleted the new files detailed information from code. So that's why. @KIKOSoftware – Sid Aug 27 '16 at 06:25

2 Answers2

0

Your code is syntactically correct, only issue i have found here is 'blank spaces' before and after php tag <?php and ?> this is a problem which does not encounter with windows systems but gives error or page blank issues on linux systems. Try removing blank spaces before and after php tag from your contact.php file.

Gaurav Chauriya
  • 304
  • 2
  • 12
  • when I tried to remove blank space from end tag and run the script got another error. Please check the edited error in question. @Gaurav Chauriya – Sid Aug 27 '16 at 08:09
0

I have checked your code that is perfect but do following things

1:remove space in starting of php tag

2:remove the closing tag of php '?>' in your class or remove space between the php closing tag and in between the } parenthesis

3:Use __construct() instead of Contact method

smehsoud
  • 312
  • 2
  • 11
  • when I tried to remove blank space from end tag and run the script got another error. Please check the edited error in question. @smehsoud – Sid Aug 27 '16 at 08:09