Access config.php variables

Asked

Viewed 1,237 times

1

I am a beginner in php and I have a problem, I have a config.php file this way:

<?php
$config['dbHostname'] = 'localhost';
$config['dbUser'] = 'teste';
$config['dbPassword'] = 'passteste';

But I have a class with connection functions and database manipulation that would use these variables from config.php however I could not access them in any way, please someone could help me?

  • Personal I’m sorry but the intention is to have a file only with settings, then in another class I will make the connection and data handling, not make the connection in the same php as the settings

  • The example is you have a really separate file, it is not logical to do in the same file and then use such file (with include or require) to have several files that will use database connection ... But, your question was not how to read such file with the variable Array $config ?

4 answers

1


It would be one of the ways, in this case by the class builder.

<?php

$config['dbHostname'] = 'localhost';
$config['dbUser'] = 'teste';
$config['dbPassword'] = 'passteste';

class Conexao
{
    private $pdo;
    public function __construct($config){
        $this->pdo = 
            new PDO("mysql:dbname=generics;host=".$config['dbHostname'], $config['dbUser'], $config['dbPassword']);
    }
}


$conexao = new Conexao($config);

?>
  • Although he is a beginner, it is always good to teach secure connection methods, but the ideal would be to show him mysqli first. And the $config variable is already an array, specify the attribute of the array by name only accumulates space in the code - I believe that the ideal would be to simplify $config[0,1,2]...

  • 1

    Personal I’m sorry but the intention is to have a file only with settings, then in another class I will make the connection and data handling, not make the connection in the same php as the settings

  • Dear G. Foratini, you can make a include of this snippet so that it is seen in the files that you will use such a configuration, in my example (as I said example) I wanted you to understand the trivial way to pass to a class, because your question was this how to read the settings of a $config which is an array type variable for my classes. In the matter of using Mysqli or PDO the material has in the site itself (www.php.net). About taking up space, I see no problem using a $config Array type, which is something frameworks have to mount ...

  • 1

    Ahh yes I understood now, thank you so much I ended up expressing myself badly worth ;)

1

You can access it by declaring the variable at the beginning of the function with keyword global

public function mostrar1()
{
    global $txt;
    echo $txt;
}

Or use the super-global $GLOBALS

public function mostrar2()
{
    $msg = $GLOBALS['txt'];
    echo $msg;
}

Example running http://ideone.com/ek9ACf

Remember that if you are programming using classes it is not very recommended to use global variables because they can cause conflicts, if you use them copy the data that interests you and remove it from the global scope.

class Config
{
    /**
     * Configurações
     */
    var $safeconfig;

    public function __construct()
    {
        global $config;

        $this->safeconfig = $config;
        unset($config);
    }

    /**
     * Adicione aqui alguns setters e getters
     * para ter acesso as configurações
     */
}

1

A simplified way to do this only with config.php would be:

<?php
    $config['dbHostname'] = 'localhost';
    $config['dbUser'] = 'teste';
    $config['dbPassword'] = 'passteste';
    $config['dbSchema'] = 'baseteste';

    $mysqli = new mysqli($config['dbHostname'],  $config['dbUser'], $config['dbPassword'], $config['dbSchema']);

    if (mysqli_connect_errno()) {
       printf("Falha na conexão: %s\n", mysqli_connect_error());
       exit();
    }

Any doubt about using the mysqli function just look at the official documentation

0

Hello use this following connection, probably you are not selecting the database use my connection but put the name of your database

<?php 
$servidor = "localhost";
$banco = "NOME DO BANCO DE DADOS AQUI";
$usuario = "teste";
$senha = "passteste";
$conexao = mysql_connect ($servidor, $usuario, $senha);
$conexao = mysql_select_db ("$banco", $conexao);
if (!$conexao) {
echo mysql_error; exit;
}
?>
  • Truth even Lucas I think he is not selecting the Bunch of data

  • 1

    The mysql_* functions have been deprecated, please do not post code that prompts the user to use them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.