繁体   English   中英

在类中加载 config.php

[英]load config.php with in a class

我想在类中加载配置文件。 这是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注入问题,我只需要完美执行代码

为什么不使用 .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;
 }

阅读PHP 手册中的变量作用域

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

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

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

您需要在要使用变量的同一范围内加载配置文件:

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

尝试这个

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM