0

基本上我要做的是将数据库访问变量(用户名、密码等)重新定位到外部 php 文件,然后将该文件包含在服务中。因此,这是我可以使用的服务代码:

<?php
/** Database Access Information */
class LoginService {
    require 'parameters.php';

var $accesstable = "access";
var $lastlogintable = "last_login";
var $loginhistorytable = "login_history";

var $connection;

/** The constructor initializes the connection to database. */

public function __construct() {
    $this->connection = mysqli_connect(
                            $this->server,  
                            $this->username,  
                            $this->password, 
                            $this->databasename,
                            $this->port
                        );

    $this->throwExceptionOnError($this->connection);
}

然后这是我认为可以在外部文件中使用的代码:

<?php

var $username = "root";
var $password = "";
var $server = "localhost";
var $port = "3306";
var $databasename = "User";

?>

但是,唉,它没有用...... Dreamweaver 一直在给我一个语法错误:

require 'parameters.php';
4

1 回答 1

0

将 require 放在方法中 - 类语法不允许在类主体中包含任何代码(但您可以在方法主体中包含任何内容)。

public function __construct() {
    require 'parameters.php'; 
    $this->connection = mysqli_connect(
                        $this->server,  
                        $this->username,  
                        $this->password, 
                        $this->databasename,
                        $this->port
                    );

        $this->throwExceptionOnError($this->connection);
}
于 2013-05-23T18:13:46.347 回答