意外的文件结束错误。语法错误。腓

时间:2016-08-27 06:06:38

标签: php syntax-error unexpectendoffile

我创建了一个php类它给了我语法错误。意外的文件结束。当我在localhost上测试它运行良好。但是,当我在服务器上测试它显示此错误。我的服务器在linux平台上,我在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;
        }
    }
}
?>

错误:

<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 />

第128行 -

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

我在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;
    }
?>

我已经完成了如何解决语法错误链接。我没有发现任何变化。我检查了大括号,标签的开头和结尾。还检查了区分大小写。但是这里没有弄错。

任何人都可以帮忙。谢谢..

2 个答案:

答案 0 :(得分:0)

您的代码在语法上是正确的,只有我在这里发现的问题是在php标记<?php?> 之前和之后的'空格'这是一个问题,它不会遇到Windows系统但是会出错或Linux页面上的空白问题。 尝试从contact.php文件中删除php标记之前和之后的空格。

答案 1 :(得分:0)

我已经检查了你的代码是完美的,但做了以下事情

1:删除启动php标签的空间

2:删除php'?&gt;'的结束标记在你的班级或删除php结束标记之间和}括号

之间的空格

3:使用__construct()代替Contact方法