Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在类中加载config.php_Php_Oop_Class_Object_Include - Fatal编程技术网

在类中加载config.php

在类中加载config.php,php,oop,class,object,include,Php,Oop,Class,Object,Include,我想在类中加载配置文件。下面是config.php的内容 <?php $__error_reporting_level=1; $server='localhost'; $user='root'; $password=''; $dbase='eauction'; ?> 我得到这个错误 无法选择数据库 忽略mysql注入问题,我只需要完美地执行代码 在类声明之前,您已经在全局范围中包含了该文件,而类方法范围无法访问该文件。如果要在类方法中使用这些变量,则需要通过$GLOBALS[]或g

我想在类中加载配置文件。下面是config.php的内容

<?php
$__error_reporting_level=1;
$server='localhost';
$user='root';
$password='';
$dbase='eauction';
?>
我得到这个错误

无法选择数据库

忽略mysql注入问题,我只需要完美地执行代码

在类声明之前,您已经在全局范围中包含了该文件,而类方法范围无法访问该文件。如果要在类方法中使用这些变量,则需要通过$GLOBALS[]或global关键字全局访问它们,或者更好地将它们传递给使用它们的函数

include ('config.php');
// Now all your variables are defined at global scope!!!
error_reporting($__error_reporting_level);

class sql
{
 // Pass as params to the function
 function connect($server, $user, $password, $dbase)
 {
  $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
  mysql_select_db($dbase) or die ('Unable to select database!');
  return;
 }
 // etc...
 // etc...
}

您也可以考虑将它们设置为类属性,并在构造函数中传递它们:

class sql
{
  public $server;
  public $user;
  public $password;
  public $dbase;

  public function __construct($server, $user, $password, $dbase) {
    $this->server = $server;
    $this->user = $user;
    // etc...
    $connections = mysql_connect($this->server, $this->user, $this->password);
  }
}

在配置中使用定义的变量

试试这个

config.php
define('SERVER',$server);
define('USER',$user);
define('PASSWORD',$password);
define('DBASE',$dbase); 



----------class----------     
<?php
include ('config.php');
error_reporting($__error_reporting_level);

class sql
{
 protected  $server=SERVER;
 protected $user=USER;
 protected $password=PASSWORD;
 protected $dbase=DBASE;
 private function connect()
 {
  $connections = mysql_connect($this->server, $this->user,$this->password) or die ('Unabale to connect to the database');
  mysql_select_db($this->dbase) or die ('Unable to select database!');
  return;
 }
........
........

为什么不使用.ini文件呢

config.ini

在你们班上有一些

class A {

    public $config;

    public function __construct() {
       $this->config = parse_ini_file('config.ini', true);
    }

    public function sql() {
        $connections = mysql_connect($this->config['server'], $this->config['user'],$this->config['password']) or die ('Unabale to connect to the database');
  mysql_select_db($this->config['dbase']) or die ('Unable to select database!');
  return;
    }
}
另一种方法是,确保数据库的名称也正确

此外,如果要使用当前的config.php,则需要在使用中的变量的方法中包含。不能从该范围之外使用它

function connect()
 {
    include ('config.php');
  $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
  mysql_select_db($dbase) or die ('Unable to select database!');
  return;
 }

我可以这样做。。代码中的问题一定是config.php中声明的变量不可访问。你可以让它全球化,或者试试我的代码

<?php
class ConnectionSettings {
    private $hostname = 'localhost';
    private $username = 'root';
    private $password = '';
    private $db = 'cordiac_db';
    protected $connLink;

    // protected 'connect()' method
    protected function connect(){

        // establish connection
        if(!$this->connLink = mysql_connect($this->hostname, $this->username, $this->password)) {
            throw new Exception('Error connecting to MySQL: '.mysql_error());
        }

        // select database
        if(!mysql_select_db($this->db, $this->connLink)) { 
            throw new Exception('Error selecting database: '.mysql_error());
        }
    }
}
试试这个

config.ini
[database]
username = root
password = 1
dbname = users
dsn = localhost
......
class Database{
public $koneksi;
  function __construct(){
    try{
        $config = parse_ini_file('config.ini'); 
        $this->koneksi = new PDO("mysql:host=" . $config['dsn'] . ";dbname=" . $config['dbname'], $config['username'],$config['password'],array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $this->koneksi->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }catch(PDOException $e){
        $e->getMessage();
    }
  }
}

您需要在中加载配置文件,以便使用变量:

class sql
{
    function connect()
    {
        include 'config.php';
        $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
        mysql_select_db($dbase) or die ('Unable to select database!');
    }
}

不能在类函数中直接使用$server、$user等。请确保数据库名称正确。数据库名称正确,兄弟!如果您生活在仅mysql的领域中,为什么还要创建数据库类而不是使用PDO或MySQLi?我想从config.php保存并加载mysql_connect参数,所以我不需要更新它!
<?php
class ConnectionSettings {
    private $hostname = 'localhost';
    private $username = 'root';
    private $password = '';
    private $db = 'cordiac_db';
    protected $connLink;

    // protected 'connect()' method
    protected function connect(){

        // establish connection
        if(!$this->connLink = mysql_connect($this->hostname, $this->username, $this->password)) {
            throw new Exception('Error connecting to MySQL: '.mysql_error());
        }

        // select database
        if(!mysql_select_db($this->db, $this->connLink)) { 
            throw new Exception('Error selecting database: '.mysql_error());
        }
    }
}
config.ini
[database]
username = root
password = 1
dbname = users
dsn = localhost
......
class Database{
public $koneksi;
  function __construct(){
    try{
        $config = parse_ini_file('config.ini'); 
        $this->koneksi = new PDO("mysql:host=" . $config['dsn'] . ";dbname=" . $config['dbname'], $config['username'],$config['password'],array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $this->koneksi->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }catch(PDOException $e){
        $e->getMessage();
    }
  }
}
class sql
{
    function connect()
    {
        include 'config.php';
        $connections = mysql_connect($server, $user,$password) or die ('Unabale to connect to the database');
        mysql_select_db($dbase) or die ('Unable to select database!');
    }
}