I can not connect to the database

1

I have this code:

<?php

class Conexion {
    private $host = "localhost";
    private $user = "root";
    private $pass = "";
    private $bd = "nutricion";
    private $con;
    public function Conectar() {
        $this->con = mysqli_connect($this->host, $this->user) or die("Error al conectar al Servidor");
        mysql_select_db($this->bd, $this->con) or die("Ha ocurrido un Error al conectar a la Base de Datos");
    }
}

?>

Which generates the following error:

  

Warning: mysqli_connect (): (HY000 / 2002): No connection could be made because of the target machine. in C: \ xampp \ htdocs \ admin \ classes \ Connection.php on line 10 Error connecting to the Server

I have tried new ways to connect the database with other codes but all of them generate an error, two years ago this code worked perfectly for me, I'm going back to the project and I'm not good at php, I hope someone can help me.

    
asked by Analy 20.04.2018 в 19:32
source

2 answers

5

If you have a problem with the use of the mysql and mysqli libraries, but those are not the reasons why the error is sending you:

  

Warning: mysqli_connect (): (HY000 / 2002): No connection could be made   because the target machine in   C: \ xampp \ htdocs \ admin \ classes \ Connection.php on line 10 Connection error   to the Server

The reason for this error is because the port of the MySQL is not accessing it, which can be for many reasons, the ones that occur to me can be because you are blocking the port or because the port is not listening more specifically they could be:

  • MySQL is turned off, check its status to make sure it's on.
  • The MySQL is not listening for the port you are indicating, the port for deafult is 3306.
  • There is a Firewall blocking local communication, if both are installed on the local host and if it is Linux it looks for your iptables, if it is windows it reviews the documentation of your firewall, you could also try to establish the connection from a terminal or console directly to the MySQL server.
  • There is a Firewall or a routing problem in your network that is not letting you get to your MySQL, check from your console or terminal to access your MySQL.

Usually if your code is the same and before it worked and now it does not work anymore, it is because something changed in the environment or because there is an exception that was not taken into account, maybe it could be:

  • Software update (PHP, MySQL, Web Server, etc.)
  • The network changed
  • There is new software
  • Finally, if the credentials were wrong, the error would be different, indicating a user and password problem, and if it were to use the functions incorrectly, there would be a different error by the "resource id" or by a depreciated function.

        
    answered by 20.04.2018 в 21:54
    1

    Just as the code will throw you error because you are mixing 2 libraries or different classes when making the connection: mysqli and mysql , the latter already obsolete.

    The correct way to connect using the class mysqli would be in your case:

    public function Conectar() {
            $this->con = mysqli_connect($this->host, $this->user, $this->pass, $this->bd) or die("Error al conectar al Servidor");
            return $this->con; // << este detalle crucial faltaba: retornar el enlace al ejecutar el método (por @Mr.Manutri)
        }
    

    A good practice is always to check the official documentation of the languages to be able to find the answer to the errors; If after that you can not find the answer, apply Google (and StackOverflow: P). Here is the specific function: PHP: mysqli_connect .

    EDITED

    Now, regarding the execution of SQL queries using mysqli , again we refer to the official documentation: PHP: mysqli_query

    The correct use of this function is, assuming we are using your class Conexion :

    <?php
    // Instancias el objeto:
    $conexion = new Conexion();
    
    // Ejecutas la conexión, y obtienes un identificador de enlace a la BD: 
    // **FAVOR Notar el cambio que hice en tu declaración de método Conectar(), más arriba.
    $enlace = $conexion->Conectar();
    
    // Acá va tu query:
    $consulta = "...";
    
    // Y acá la ejecutas: debes incluir el identificador de enlace a la BD para que funcione correctamente:
    $resultado = mysqli_query($enlace, $consulta);
    ?>
    

    Try it this way, and tell us.

        
    answered by 20.04.2018 в 19:41