php to establish connection to the database

4

hello community I'm just learning the object-oriented programming for reasons of scalability in my application and well I have this code to establish connection to the database ..

file config.php

$host="localhost";
$user="root";
$pass="";
$DBname="login";

and lugeo conexion.php

   require"config.php";

  class conexion{

     public function dbConnect(){


         $conexion= new mysqli($host,$user,$pass,$DBname);


         if (!$this->conexion) {


             echo"conxion width db fail: ".$this->conexion->coonect_error;
             return;
         }


     }


  }

launches the following error ...

  

Notice: Undefined variable: host in   C: \ xampp \ htdocs \ pooClass_php \ conexion.php on line 16

     

Notice: Undefined variable: user in   C: \ xampp \ htdocs \ pooClass_php \ conexion.php on line 16

     

Notice: Undefined variable: pass in   C: \ xampp \ htdocs \ pooClass_php \ conexion.php on line 16

     

Notice: Undefined variable: DBname in   C: \ xampp \ htdocs \ pooClass_php \ conexion.php on line 16

     

Notice: Undefined variable: conn in   C: \ xampp \ htdocs \ pooClass_php \ index.php on line 30

someone who can guide me a little and can help me understand some concepts? thanks!

    
asked by andy gibbs 09.09.2018 в 00:33
source

2 answers

3

It happens that the variabels are not within the declaration of your class, although you have them in an external file;

  

I have removed the PDO sample.

I update to place an example with mysqli with a separate file, for this we will create a config file called config.php.ini to have the credentials in an external file;

config.php.ini

<?php return; ?>
; credenciales
host=localhost
usuario=elusuariodeladb
clave="laclave"
dbnombre=elnombredeladb

Now our class connection, in the same note that you do not have to call the function require or include since the function parse_ini_file is responsible for reading the file and assigning its value to the property credentials; conexion.php

class conexion {
    //Esta vez solo necesitaremos una propiedad que es de tipo array
    private $credenciales;
    protected $myconn;

    function connect() {
       $this->credenciales = parse_ini_file("config.php.ini");
        $con = mysqli_connect($this->credenciales["host"], $this->credenciales["usuario"], $this->credenciales["clave"], $this->credenciales["dbnombre"]);
        if (!$con) {
            die('no se ha podido conectar con la base de datos!');
        } else {
            $this->myconn = $con;
            echo 'exitosa';}
        return $this->myconn;
    }

    function close() {
        mysqli_close($myconn);
        echo 'conexion cerrada';
    }

}
    
answered by 09.09.2018 / 00:43
source
2

Here is an example with mysqli

  

Consider that if you are creating a script that will serve to make   a connection to a server for example of databases; so he   appropriate access modifier in this case should be private then   they will only be available within the class that owns them

<?php

class Conexion 
{
    private $host;
    private $user;
    private $pass;
    private $dbname;

    public function __construct($host, $user, $pass, $dbname)
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->dbname = $dbname;
    }

    public function createConecction()
    {
        $conecta = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
        return ($conecta) ? 'conectado' : 'no conectado';
    }
}

$obj = new Conexion("localhost", "root", "password", "demo");
echo $obj->createConecction(); //imprimirá conectado
  

The first thing I do is declare the properties, later   I declare a magical method construct() to initialize the   properties when the class is created; finally I create a method   personalized that is the one that contains the call to the   mysqli

     

I also make the observation, the properties that you will use the   you must declare within the scope of the class to recognize them   as such when you want to use them; so I do not see it necessary that the   declare in another file and then invoke it

    
answered by 09.09.2018 в 01:02