我在尝试输出时在浏览器上收到错误消息

时间:2013-12-27 02:23:10

标签: php

这就是错误说的解析错误:语法错误,意外'?> ',期待功能(T_FUNCTION)

class DB {
    protected $db_name = 'nklopP';
        protected $db_user = 'root';
        protected $db_pass = 'nyiyfyfy2013';
        protected $db_host = 'localhost';

        //open a connection to the database. Make sure this is called
        //on every page that needs to use the database.
        public function connect() {
             $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
            mysql_select_db($this->db_name);

            return true;
        }           

2 个答案:

答案 0 :(得分:1)

这听起来像你的完整代码(由于某些原因你不会显示),看起来像这样:

<?php

class DB {
    protected $db_name = 'nklopP';
        protected $db_user = 'root';
        protected $db_pass = 'nyiyfyfy2013';
        protected $db_host = 'localhost';

        //open a connection to the database. Make sure this is called
        //on every page that needs to use the database.
        public function connect() {
             $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
            mysql_select_db($this->db_name);

            return true;
        } 
?>

解决方案是这样的:

<?php
class DB {
    protected $db_name = 'nklopP';
        protected $db_user = 'root';
        protected $db_pass = 'nyiyfyfy2013';
        protected $db_host = 'localhost';

        //open a connection to the database. Make sure this is called
        //on every page that needs to use the database.
        public function connect() {
             $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
            mysql_select_db($this->db_name);

            return true;
        }
    } // <---- fix your syntax errors!
?>

但是,如果你没有显示所有相关代码,那就无法确定了。

答案 1 :(得分:1)

如果你想采用OOP风格,你应该考虑像这样做你的DB类:

<?php
class DB
{
    protected $db_name = 'nklopP';
    protected $db_user = 'root';
    protected $db_pass = 'nyiyfyfy2013';
    protected $db_host = 'localhost';

    protected $link;

    //open a connection to the database. Make sure this is called
    //on every page that needs to use the database.
    public function connect() 
    {
        $this->link = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);

        return true;
    }   
} // This is most likely where you went wrong, because you forgot to end the class.     

?>