Php 试图输出此信息时,我在浏览器上收到一条错误消息

Php 试图输出此信息时,我在浏览器上收到一条错误消息,php,Php,这就是错误所说的解析错误:语法错误,意外的“?>”,应为函数T_ class DB { protected $db_name = 'nklopP'; protected $db_user = 'root'; protected $db_pass = 'nyiyfyfy2013'; protected $db_host = 'localhost'; //open a connection to the database. M

这就是错误所说的解析错误:语法错误,意外的“?>”,应为函数T_

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;
        } 
?>
<?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!
?>
解决方案如下:

<?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!
?>

但是如果你不显示所有相关的代码,就不知道了。

如果你要面向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.     

?>

除非我遗漏了它,否则你没有包含相关的代码。我已经做了一些编辑。请现在查看其中的代码?>在其中。。。这就是为什么要使用带有mysql_*函数的类的问题?tsk tskfred我不明白你的评论嘿,我们都假设了同一件事。我试着让他展示所有的代码是的,看来我们得出的结论是正确的,因为他没有结束课程,所以考虑到?>仍然在课堂上。哈哈,我想我有点累了,谢谢m59,我在哪里点击接受answer@ochieng答案左边的分数下面有一个复选标记。非常感谢。最后一个问题mysql和mysqliMySQL之间的区别是什么MySQLi与所有类一起被斩首,更新后的库中充满了安全性较低的新类risks@ochieng^是的。PDO有一些我比mysqli更喜欢的东西,比如命名参数。