Differentiates or benefits of connecting to a class based on data

0

Today I come with a very basic question about connection to data base since I do not have some clear concepts and would like to know what is the difference or benefit of connecting to the database using a class or normally.

That is, what would be the difference of this code :

<?php 
     class conexion {

    /**
    * Gestiona la conexión con la base de datos
    */

    private $dbhost = 'localhost';

    private $dbuser = 'nombre usuario';

    private $dbpass = 'contraseña';

    private $dbname = 'base de datos';

    public function conexion () {

    /**
    * @return object link_id con la conexión
    */

    $conn= new mysqli($this->dbhost,$this->dbuser,$this->dbpass,$this->dbname);

    if ($conn->connect_error) {

    echo "Error de Connexion ($conn->connect_errno)

    $conn->connect_error\n";

    header('Location: error-conexion.php');

    exit;

    } else {

    return $conn;

    }

    }

    } ?>

just make this code:

<?php 


  $conn= new mysqli("localhost","root","","DB");

  if($conn){

  echo"conexion fail"; 
}else{

    echo"conexion success";
}

?>

More than a question, it is a doubt, that is why the first code would be better?

I hope you can clarify this simple doubt and apologize for the ignorance but until now I had not given much importance to the connection with the database, thanks!

    
asked by andy gibbs 06.09.2018 в 20:16
source

2 answers

0

You can create your connection in a file called conex.php where you put the data of your database, example:

$password='root';
$servidor='localhost';
$bd_name = 'asteriskcdrdb';

$conexion = mysqli_connect($servidor, $usuario, $password) or die('No se pudo conectar: ' . mysqli_error());
mysqli_select_db($conexion, $bd_name) or die('No se pudo seleccionar la base de datos'. mysqli_error());

And call it in your class where you need to make a process with your database such as an insert, update ... Example in your file add_user.php invoke your connection.

require_once('conexion.php');   

And the other way is to create the connection again in your class add_user.php which is not practical since you are not working with object-oriented programming. When you work with object-oriented programming you reuse a lot of code.

    
answered by 06.09.2018 в 20:47
-2

A good practice and why a class is used to represent the connection to the database is to be able to perform a single connection to the bd in the whole application and represent it using a singleton class is to say that it is not instantiated again each time it is used but that it exists permanently throughout the life cycle of the application.

Another very common reason in object-oriented languages is to be able to represent the database connection as an object which can be integrated into more complex architectures in an easy and orderly manner, such as multiple connections to data or create connection interfaces that today be towards a database and tomorrow towards a rest api.

One last thing I can think of is to facilitate automatic testing, you can call the connection as an instance during the execution of a specific test and encapsulate the actions as connection disconnection and query under instance methods.

    
answered by 06.09.2018 в 20:22