在类中加载config.php

时间:2012-02-28 03:47:23

标签: php oop class object include

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

<?php
$__error_reporting_level=1;
$server='localhost';
$user='root';
$password='';
$dbase='eauction';
?>

sql.php的内容

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

class sql
{
 function connect()
 {
  $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;
 }

 function login($email, $pwd)
 {
  $this->connect();
  $result = $this->qry("SELECT uid,nameF FROM user WHERE email='".$email."' AND password='".$pwd."'");
  $row=mysql_fetch_array($result);
  if (mysql_num_rows($result)>0)
   return array($row[0],$row[1]);
  else
   return array(0,0);
 }
}
?>

我使用

执行代码
include ('core/sql.php');
$obj = new sql;
$data=$obj->login($email,$pwd);
print_r($data);

我收到此错误

无法选择数据库!

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

7 个答案:

答案 0 :(得分:12)

为什么不使用.ini文件?

<强> 的config.ini

server=test
user=root
pass=pass
dbname=mydb

并且在课堂上有类似

的内容
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;
 }

答案 1 :(得分:5)

阅读variable scope in the PHP manual

您已在类声明之前包含该文件,在全局范围内,类方法范围无法访问。如果要在类方法中使用这些变量,则需要通过$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);
  }
}

答案 2 :(得分:2)

在配置中使用已定义的变量。

http://php.net/manual/en/function.define.php

答案 3 :(得分:1)

试试这个

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;
 }
........
........

答案 4 :(得分:1)

可以这样做..代码上的问题必须是你在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());
        }
    }
}

答案 5 :(得分:0)

试试这个

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();
    }
  }
}

答案 6 :(得分:0)

您需要在要使用变量的same scope中加载配置文件:

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!');
    }
}