简体   繁体   English

在类中加载 config.php

[英]load config.php with in a class

I want to load a configuration file in a class.我想在类中加载配置文件。 Here is the content of config.php这是config.php的内容

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

Content of sql.php 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);
 }
}
?>

I execute the code using我使用执行代码

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

I get this error我收到这个错误

Unable to select database!无法选择数据库!

Ignore mysql injection issue, I just need to execute the code perfectly忽略mysql注入问题,我只需要完美执行代码

Why not use a .ini file?为什么不使用 .ini 文件?

config.ini配置文件

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

and with in your class have something like在你的课堂上有类似的东西

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

just another way of doing it, make sure your database is correctly named too.只是另一种方法,请确保您的数据库也正确命名。

Additionally, if you want to use your current config.php then you will need to include in the method you are using the variables in. It cannot be used from outside that scope.此外,如果您想使用当前的 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;
 }

Read up on variable scope in the PHP manual .阅读PHP 手册中的变量作用域

You've included the file before the class declaration, at global scope, not accessible to the class method scope.您已在类声明之前包含该文件,在全局范围内,类方法范围无法访问。 If you want to use those variables inside class methods, you'll need to either access them globally via $GLOBALS[] or the global keyword, or better yet, pass them into the function that uses them.如果要在类方法中使用这些变量,则需要通过$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...
}

You might also consider setting them up as class properties, and passing them in the constructor:您还可以考虑将它们设置为类属性,并将它们传递到构造函数中:

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

try this尝试这个

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

use defined variables in your config.在您的配置中使用定义的变量。

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

You need to load the config file in the same scope that you want to use the variables:您需要在要使用变量的同一范围内加载配置文件:

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

Try this尝试这个

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

can do it this way.. the issue on your code must be that the variable you declare in config.php is not accessible.可以这样做..您的代码问题必须是您在 config.php 中声明的变量不可访问。 you can make it global or try this code of mine.您可以将其设为全局或尝试我的此代码。

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