Can not connect to MySQL server on 'localhost' (10061)

-1

I need help I am making a PHP connection with Mysql and it appears to me that it can not be done.

I get this error:

  

Can not connect to MySQL server on 'localhost' (10061).

Use:

<?php
$conexion = new mysqli('localhost','root','root','prueba');

print_r($conexion);

if($conexion->connect_errno)
{
echo "no conexion";
}
else{
  echo "si conexion";
}
?>
    
asked by Bre Morales 17.01.2018 в 02:01
source

2 answers

0

To find out the specific error, try to place your code inside a try and catch, something like this:

$servername = "localhost";
$username = "username";
$password = "password";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {
  die("Fallo la conexion: " . $conn->connect_error);
}

echo "Conexion correcta";
    
answered by 17.01.2018 в 02:22
0

If you have access to the server you can try to connect from the MySQL client to check if the credentials are correct and if it will not show you more errors, otherwise put a code like the following to catch the error.

  $conn = new mysqli();
  $conn->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if($conn->connect_error)
     die("Error: " . $conn->connect_error . " - Numero : " . $conn->connect_errno);
    
answered by 17.01.2018 в 22:40