Will not let me connect to mysql using mysqli

-1

The code is as follows:

$logID = checkParam($_GET["id"]);
$database = mysqli_connect($config["db_server"], $config["db_user"], $config["db_password"], $config["db_name"]);

if($database)
{
    if($logID != null)
    {
        $log = $database->query("SELECT * FROM 'logs' WHERE 'id'='$logID';")->fetch_array();

        if($log["id"] != null)
        {
            $fpath = "./server/". $config["logs_folder"]."/". $bot["user"]."".$bot["hwid"].".zip";
            unlink($fpath);

            $database->query("DELETE FROM 'logs' WHERE 'id'='$logID'");
        }

        header('Location: http://'. $_SERVER["HTTP_HOST"] .'/index');
    }
}
else
{
    echo "Can't connect to MySQL";
    exit(0);
}

But when I do I get the following error:

  

Can not connect to MySQL

What should I do? I change everything to POO or because it does not connect me with mysqli?

    
asked by Sergio Ramos 02.05.2018 в 21:14
source

2 answers

0

It is likely that the MySQL server is not listening where you try or some other parameter is not correct.

For example the server only listens in local (localhost and / or 127.0.0.1) and you are accessing from another machine. You may also try to access by IP and only accept sock connections. Or that the port is not the same one in which the server is listening. It may also be that the user or password is not correct. Or that the user is only associated with a specific host and is not accessible.

There is a complete list of possible connection problems in MySQL. link

But not having the real error that appears is difficult to know what really happens. You can use the MySQLi error functions in php to get the code and error message:

mysqli_connect_errno(); and mysqli_connect_error();

With this information it will be easier to know what is happening and act accordingly.

    
answered by 02.05.2018 в 21:46
0

Declare your connection in another file: conexion.php

<?php

  DEFINE ('DB_USUARIO','user');
  DEFINE ('DB_CLAVE','pass');
  DEFINE ('DB_HOST','localhost');
  DEFINE ('DB_NOMBRE','database');
   $con = mysqli_connect(DB_HOST,DB_USUARIO,DB_CLAVE,DB_NOMBRE);
   mysqli_set_charset($con, "utf8");
 ?>

And where are you going to use it in your query files etc.? File consulta.php

include('conexion.php');

To do the operations, you do it in the following way:

$result  = mysqli_query($con,'SELECT * FROM tabla') or mysqli_error($con);
    
answered by 02.05.2018 в 23:05